I know that we can parse a CSV line ( ';' separator ) with something like that :
delim = ';'
myline="i;want;to;know;what;love;is"
parse var myline w1 (delim) w2 (delim) w3 (delim) w4 (delim) w5 (delim) w6 (delim) w7
say w1 w2 w3 w4 w5 w6 w7
I want to know if there is a way to simplify the iteration of the 'w# (delim)'in order to do something like :
parse var myline w1 (delim) w2 (delim) ... (delim) w6 (delim) w7
/* then we will have all w1 to w7 defined
I could do a function with some array to do that but is it possible natively on rexx, i was just wondering
Thanks
Sure, if what you really want is n
variables named w1
, w2
, ... wn
:
do i = 1 by 1 while myline <> ''
parse var myline word (delim) myline
interpret "w" || i "= word"
end
But the Rexx-y way to do this is using a "stem array":
delim = ';'
do i = 1 by 1 while myline <> ''
parse var myline w.i (delim) myline
end
w.0 = i-1
or even:
do i = 1 by 1 while myline <> ''
parse var myline w.i ';' myline
end
w.0 = i-1
When you're done, you have an array w.
with it's count in w.0
and the n
words in w.1
, w.2
, ... up through w.n
. This is better because Rexx has special handling for the part following the .
: you can use any variable, and its value will be used instead. So printing all those words is just:
do i = 1 to w.0
say w.i
end
Or re-assembling them is just:
line = ""
do i = 1 to w.0
line = line || w.i || ';'
end