Home   Archive   Permalink

Is there a way to parse/all with '|' and ignore double or triple quotes?

I get long strings from an external source that are pipe-delimited; some of the fields can contain 2 or even 3 double quotes in a row, and parse/all is still seeing these as empty field boundaries. For example:
>>tmp: {a|bc|"d,e"|""something"more"|g}
>> parse/all tmp {|}
== ["a" "bc" "d,e" "" {something"more"} "g"]
what I *want* is just breaks on the pipes:
["a" "bc" "d,e" {""something"more"} "g"]
Is there a way to do that with parse?
Many thanks in advance!

posted by:   JackKort       13-Mar-2018/12:24:10-7:00

Simple string splitting does not help here, but you can roll your own.
>> out: copy [] parse/all tmp [any [copy val to "|" (append out val) skip ] copy val to end (append out val)]
== true
>> out
== ["a" "bc" {"d,e"} {""something"more"} "g"]

posted by:   Ingo       13-Mar-2018/14:40:31-7:00

P.S. depending on your version, if your last element is a delimiter, you'll get none or an empty string as the last element.

posted by:   Ingo       13-Mar-2018/14:57:15-7:00

Wow! Seems to work great, and very concise. Thank you very much, Ingo!

posted by:   JackKort       13-Mar-2018/17:26:55-7:00



Type the reverse of this captcha text: "y r a r b i l - o t"