{
:local ver [/system resource get version]
:put [:pick $ver 0 [:find $ver " "]]
}
:local ver [/system resource get version] Get the line [:find $ver " "] gets position of space [:pick $ver 0 [:find $ver " "]] get text from position 0 to first space
find :find return position of substring or array element :put [:find “abc” “a” -1]
pick :pick [] return range of elements or substring. If end position is not specified, will return only one element from an array. :put [:pick “abcde” 1 3]
It would be nice when the scripting language was extended with some features known from e.g. Perl, like “split” and “regexp match with capture”.
These operations would take a line of characters and either a splitting character (like a space in your case) or a regular expression describing the line and what you want to get from it, and return the results as an array holding the extracted fields.
Until then, indeed you will have to use (possibly repeated) use of find and pick.
Using :toarray “trick” to parse a delimated string doesn’t know about | being a separator. By “cycle”, that means a “:for” loop with a :pick and :find inside…since :toarray isn’t really a parser, it just conveniently use comma in syntax to define elements .
I can (uselessly) write a method for not use at all pick for split a string, but if pick exist, why not use it?
For convert x|y|z on one array of 3 elements, two way are simpler: first replace inside the string all | with , and use :toarray
or use a “while” until you do not process the string from the start to the end and create a new string inside the array fore each | and the last at the end of the string.
Normally the reason to use some separator is because it is known that this separator cannot occur in the data.
I often use but in this case apparently | was chosen. I would not want to change all | to , and then use :toarray, it may well be that some fields between the | separators can have , in the value, and chaos would result.
What we really need is script functions that can convert a line to an array using user-defined separator or better: regexp with capture.
At one time we were promised a script library with useful functions like that…
FWIW, I put in a feature request on the v7.13beta’s :serialize and :deserialize to add support for “delimited file” (CSV/TSV/etc). While the new commands add JSON in beta, they do support a from= and to=…
But… I’d agree a :regexp command (with capture/grouping) that be similar to sed be useful & also solve parsing string more generically.
Remember that this is a recipe for “injection attacks” similar to the wellknown “SQL injection attacks” that are so often used to hack websites, online stores, etc., and also like using eval() to quickly parse JSON data into JavaScript.
This can really only be used when the data to be parsed can be fully trusted, not when there is any possibility that an outsider could modify the data e.g. via a form or some other data entry.