ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am in a bit of a pickle over this one. I have arrays of strings I need to pass to a function. The problem is that when an array element has a space, this is now treated as a new array element, due to the echo-ing process, parameter-passing paradigm.
function myArrayFunction ()
{
LIST=( `echo "$1"` )
echo "Array size is ${#LIST[*]}"
for A in $(seq 0 $((${#LIST[*]} - 1))); do
echo "${LIST[$A]}"
done
}
You've emptied out the contents of an array with 3 elements into a string called 'argument'. Then, using the white space, you've split the string up into 5 separate "words" and asigned them one by one to the array called 'LIST'.
It would be simpler if you avoided all that unnecessary juggling from one variable to another:
Code:
#!/bin/bash
myarray=( "one 1" "two2" "three 3")
function myArrayFunction () {
echo "Array size is ${#myarray[*]}"
for A in $(seq 0 $((${#myarray[*]} - 1))); do
echo "${myarray[$A]}"
done
}
echo "Initial array size is ${#myarray[*]}"
myArrayFunction
Yes, I understand what this is doing. What I am asking is that I will be passing several arrays to MyArrayFunction, each with different sizes and one or more spaces in the elements.
I could explicitly type in the array as you suggest, but as a C programmer, I like to avoid repeating code and potential typing mistakes in a large script.
$ declare -f afnc
afnc ()
{
local args=($(IFS='
';printf "%s\n" "$@"));
printf "Array size is %d\n" ${#args[@]};
for ((i=0; i<${#args[@]}; i++))
do
printf "%s\n" "${args[i]}";
done
}
$ array=("o n e" two "thr ee" "fo ur")
$ afnc "${array[@]}"
Array size is 4
o n e
two
thr ee
fo ur
With zsh it would be easier.
Last edited by radoulov; 01-04-2008 at 04:44 PM.
Reason: Modified.
$ afnc (){
> printf "Array size is %d\n" "${#@}";
> printf "%s\n" "$@"
> }
$ array=("o n e" two "thr ee" "fo ur")
$ afnc "${array[@]}"
Array size is 4
o n e
two
thr ee
fo ur
Here is a similar script I wrote to use ls to fill an array.
#!/bin/bash
IFS=$'\n'
### VERY IMPORTANT - changes break point for arrays!!! no break on spaces
ArrayFiles=(`ls /Data/Writer/Linux-Computers/ --format=single-column -p | grep -v /`)
# what's it do?
# ls --format=single-column ===> output is a single column, not folded multiple columns
# ls -p ===> output appends the "/" to the end of any directory names
# grep -v / ===> means select all EXCEPT lines with / --- this means ignore the directories
# echo the element count
element_count=${#ArrayFiles[*]}
echo "There are $element_count elements in the array"
# make a loop to show the records
i="0"
while [ $i -lt $element_count ]
do
echo $i \> ${ArrayFiles[$i]}
# step by 1
i=$[$i+1]
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.