I would like to affect an exploded string into an array.
one:two::four
into an array:
a[0] => one
a[1] => two
a[2] =>
a[3] => four
Quite simple in other languages with functions like explode() or split().
The best I could come up with was this:
until [ "$token" = "$string" ]
do
token=${string%%:*} # takes the first token
a[$i]=$token # affects the token to the array element
string=${string#*:} # chops that token from the string
((i++))
done
This return what I was looking for but I find it quite heavy for something basic.
#!/bin/ksh
Str=one:two::four
IFS=:
set -A Test $Str
let count=${#Test[@]}
let i=0
while [[ $i -lt $count ]]; do
print "Test[$i] ==> ${Test[$i]}"
((i=i+1))
done
@ennstate
Works fine in ksh but not in bash (I should have mentioned I was looking for a bash solution). Bash doesn't support the -A option for the set built in command. The only way I found to assign values to an array is array=(one two tree). And doing IFS=:; array=(one:two::four) doesn't work either.
@Deal_NoDeal
Also skips the empty fields as it returns
a[0] => one
a[1] => two
a[2] => four
string=one:two::four
unset a
while :
do
case $string in
*:*) a[${#a[@]}]=${string%%:*}
string=${string#*:}
;;
*) a[${#a[@]}]=${string%%:*}
break ;;
esac
done
hello every one,
I want to convert an array into list
for example the file is "agt
aag"
I need to conert it into
a
g
t
a
a
g
how I can do that in Perl?
please I need help