swankgd
February 26, 2009, 12:34pm
1
I've created a series of arrays named as follows:
row1[]
row2[]
row3[]
.
.
.
row10[]
Each has 4 elements.
I'm trying to echo the array elements out in a for loop. Here's what I have:
for ((i=1;i<=10;i++))
do
for ((j=1;j<=4;j++))
do
eval out=${row`echo $i`[$j]}
echo -n $out
done
echo
done
I get the bad substitution error on the eval out= assignment. Oddly, just a few lines before that, almost the exact same syntax works fine for assigning the values to the arrays ( eval row`echo $i`[$j]=......)
Any help is much appreciated.
swankgd:
I've created a series of arrays named as follows:
row1
row2
row3
.
.
.
row10
Each has 4 elements.
I'm trying to echo the array elements out in a for loop. Here's what I have:
for ((i=1;i<=10;i++))
do
for ((j=1;j<=4;j++))
do
eval out=${row`echo $i`[$j]}
echo -n $out
done
echo
done
i=1
while [ $i -le 10 ]; do
j=0
while [ $j -le 4 ]; do
eval "printf '%s\n' \"\${row$i[$j]}\""
printf "%s" "$out"
j=$(( $j + 1 ))
done
i=$(( $i + 1 ))
done
Simpler is:
i=1
while [ $i -le 10 ]; do
eval "printf '%s\n' \"\${row$i[@]}\""
i=$(( $i + 1 ))
done
I get the bad substitution error on the eval out= assignment. Oddly, just a few lines before that, almost the exact same syntax works fine for assigning the values to the arrays ( eval row`echo $i`[$j]=......)
Perhaps you can adapt this function to your needs:
## Usage: printarray arrayname
## Example: i=3; printarray row$1
printarray()
{
eval "printf '%s\n' \"\${$1[@]}\""
}
swankgd
February 26, 2009, 2:01pm
3
That's almost got it. How would I get it to print a tab character between each row element?
Use \t instead of \n in the format string.