Menu

#700 Add support for quotes to 'word' and 'words' functions

Version 6
closed-accepted
nobody
None
5
2025-07-21
2014-08-29
No

Hi,

the attached patch add support for quotes to the word and words functions. That means, that one can use something like

str = "one 'second word' three"
print word(str, 2)

which prints second word.

See the attached demo file for more test cases, including handling of wrapped quotes and more special cases.

2 Attachments

Discussion

  • Christoph Bersch

    Some more explanations:

    • The code doesn't recognize escaped quote characters, but e.g. single quotes can be nested inside of double quotested inside of double quotes:

      s = "A \"'quoted string'\""
      do for [i=1:words(s)]{ print word(s,i)}
      

      The output is:

      A
      'quoted string'
      
    • Another thing is, that starting quotes are recognized only when they appear at the string start, or after a white space.

      s = 'A str"ing'
      do for [i=1:words(s)]{ print word(s,i)}
      

      The output is:

      A
      str"ing
      

      As closing quote, however, the next matching character is selected:

      s = 'A "str"ing'
      do for [i=1:words(s)]{ print word(s,i)}
      

      The output is:

      A
      str
      ing
      

    So, the code should handle many practical situations.

     
  • Ethan Merritt

    Ethan Merritt - 2014-09-01

    I am not so sure this is a good idea.
    Doesn't it conflict with processing apostrophes in a normal text string?
    And doesn't it conflict with existing processing of text in data files?

    E.g.

    $DATA << EOD
    #Passengers per car
    Day  Mine Joanna's Ricky's Rent-a-car
    1      3    2      2       7
    2      2    2      4       6
    3      1    2      2       5
    EOD
    
    set key autotitle columnhead
    set format x "Day %g"
    set xtics 1
    
    plot for [car in "Joanna's Ricky's Mine"] $DATA using 1:car
    
     
  • Christoph Bersch

    Yes, I agree that this isn't 100% compatible. But processing of apostrophes is not a problem, your example works fine.

    s = "Bernadettes' Joanna's Ricky's"
    do for [w in s]{ print w }
    

    Output is:

    Bernadettes'
    Joanna's
    Ricky's
    

    Problematic would be if you have an apostrophe at the beginning of a word.

    On the other hand, with the current implementation it isn't possible to keep something together which is separated by spaces. With the patch, the follwing works fine:

    $DATA << EOD
    #Passengers per car
    Day  Mine Joanna's Ricky's "Rent a car"
    1      3    2      2       7
    2      2    2      4       6
    3      1    2      2       5
    EOD
    
    set key autotitle columnhead
    set format x "Day %g"
    set xtics 1
    
    plot for [car in "Joanna's Ricky's Mine \"Rent a car\""] $DATA using 1:car
    

    To avoid problems with apostrophes, one could also think about allowing only double quotes.

     
  • Karl Ratzsch

    Karl Ratzsch - 2014-09-03

    I think this is another problem that cries for my old wish "array variables" as ideal solution. ;-)

     
  • Ethan Merritt

    Ethan Merritt - 2014-09-07

    OK.
    Could you please add an update for the documentation?

     
  • Christoph Bersch

    Hi,

    here is an update for the documentation and the stringvar demo. Feel free to correct my english :)

    Christoph

     
  • Ethan Merritt

    Ethan Merritt - 2014-09-09
    • status: open --> closed-accepted
     

Log in to post a comment.

MongoDB Logo MongoDB