Credits | Overview | Plotting Styles | Commands | Terminals |
---|
word("string",n) returns the nth word in string. For example, word("one two three",2) returns the string "two".
words("string") returns the number of words in string. For example, words(" a b c d") returns 4.
The word and words functions provide limited support for quoted strings, both single and double quotes can be used:
print words("\"double quotes\" or 'single quotes'") # 3
A starting quote must either be preceded by a white space, or start the string. This means that apostrophes in the middle or at the end of words are considered as parts of the respective word:
print words("Alexis' phone doesn't work") # 4
Escaping quote characters is not supported. If you want to keep certain quotes, the respective section must be surrounded by the other kind of quotes:
s = "Keep \"'single quotes'\" or '\"double quotes\"'" print word(s, 2) # 'single quotes' print word(s, 4) # "double quotes"
Note, that in this last example the escaped quotes are necessary only for the string definition.
split("string", "sep") uses the character sequence in "sep" as a field separator to split the content of "string" into individual fields. It returns an array of strings, each corresponding to one field of the original string. The second parameter "sep" is optional. If "sep" is omitted or if it contains a single space character the fields are split by any amount of whitespace (space, tab, formfeed, newline, return). Otherwise the full sequence of characters in "sep" must be matched.
The three examples below each produce an array [ "A", "B", "C", "D" ]
t1 = split( "A B C D" ) t2 = split( "A B C D", " ") t3 = split( "A;B;C;D", ";")
However the command
t4 = split( "A;B; C;D", "; " )
produces an array containing only two strings [ "A;B", "C;D" ] because the two-character field separator sequence "; " is found only once.
Note: Breaking the string into an array of single characters using an empty string for sep is not currently implemneted. You can instead accomplish this using single character substrings: Array[i] = "string"[i:i]
join(array, "sep") concatenates the string elements of an array into a single string containing fields delimited by the character sequence in "sep". Non-string array elements generate an empty field. The complementary operation split break extracts fields from a string to create an array. Example:
array A = ["A", "B", , 7, "E"] print join(A,";") A;B;;;E
trim(" padded string ") returns the original string stripped of leading and trailing whitespace. This is useful for string comparisons of input data fields that may contain extra whitespace. For example
plot FOO using 1:( trim(strcol(3)) eq "A" ? $2 : NaN )