Register
It is currently Fri Oct 24, 2014 9:25 am

Array dereferencing - bad substitution error


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Feb 26, 2009 11:03 am   

Joined: Thu Feb 26, 2009 10:55 am
Posts: 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:
Code:
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.


Top
 Profile  
 PostPosted: Fri Feb 27, 2009 6:47 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I had a few goes and I don;t seem to be able to get that type of subsitution working. however this may help.
maybe change the first for loop like so;

Code:
for i in $(bash -c "echo row{$(seq -s , 10)}")
do
  for ((j=1;j<=4;j++))
  do
    eval out=${$i[$j]}
    echo -n $out
  done
  echo
done


it will loop through row1 row2 row3 etc
i haven't tested it.

looks (and is) rather complex but it enables you to still have a variable number of rows

i hope this helps


Top
 Profile  
 PostPosted: Mon Mar 02, 2009 4:08 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Something like:
Code:
row2=(foo bar baz else)
for i in row{1..10}
   do
      echo -n "row$((h+=1)):";
      for ((j=0; j<4; j++))
         do
             eval out=\${${i}[${j}]} # is more writable than '${'"${i}"'['"${j}"']}', but still doesn't change the result
             echo -n "$out"
      done
   echo
done
but it's not yet perfect, as this outputs:
Code:
row1:   
row2: foobarbazelse
row3:   
row4:   
row5:   
row6:   
row7:   
row8:   
row9:   
row10:

...
?


Top
 Profile  
 PostPosted: Wed Mar 04, 2009 8:53 am   

Joined: Wed Mar 04, 2009 8:38 am
Posts: 2
If I interpret you correctly you're basically trying to make and use a 2d array? If that's the thing it's easier to fake it all into one 1d-array in my experience.

For instance:
Code:
#!/bin/bash
width=3   #0-3
height=9  #0-9
declare -a item

# Assigning values
for row in $(seq 0 $height)
do
  for col in $(seq 0 $width)
  do
    item[$(($row*$(($width+1))+$col))]="something$row$col"
  done
done

# Direct assigning (<row number> * <width> + <col number>)
item[0*4+0]="foo"
item[5*4+1]="bar"
item[3*4+3]="moo"


# Output space delimited as a table
for row in $(seq 0 $height)
do
  for col in $(seq 0 $width)
  do
    echo -n "${item[$(($row*$(($width+1))+$col))]} "
  done
  echo
done

# Output space delimited
echo "${item[@]}"



Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP