Hi,
Is there any way to convert a string into an array in KSH? In other words I want to split the string like this:
STRING="one two three four"
into an array of 4 values splitting on white space. The array should be similar to the one that would be created with the following command:
set -A STRING "one two three four"
Is there any way to do it in one instruction, not using a loop like this one:
i=0
for WORD in `echo ${STRING}`; do
STRING2[$i]=$WORD
((i=i+1))
done
set -A array $(echo $STRING)
Well, I don't understand this thread. First of all:
$ set -A array "one two three four"
$ echo ${array[0]}
one two three four
$
so the command given in the OP results in an array with a single element. To replicate that behavior:
$ string="one two three four"
$ set -A array "$string"
$ echo ${array[0]}
one two three four
$
but if you want to split it into separate array elements do:
$ string="one two three four"
$ set -A array $string
$ echo ${array[0]}
one
$
if this is not working for you, you probably have IFS set wrong:
$ IFS=""
$ string="one two three four"
$ set -A array $string
$ echo ${array[0]}
one two three four
$
1 Like
You've been very helpful. Thank you very much!
Pit