Register
It is currently Thu Aug 21, 2014 7:57 pm

Help with array


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Wed Mar 28, 2007 8:36 am   

Joined: Wed Mar 28, 2007 8:31 am
Posts: 4
I am trying to use a value of an array as a counter. How do I increment the value in the array? Every time I try to add 1 to it or increment it, I get a "line 95: =1: command not found" error.

here's what the statement looks like:

${countarray[$index]}=`expr ${countarray[$index]} + $single`

Any help would be appreciated! :?


Top
 Profile YIM  
 PostPosted: Mon Apr 30, 2007 3:07 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Wow, this one's been unanswered for a while. Did you ever figure this out?


Top
 Profile YIM  
 PostPosted: Mon Apr 30, 2007 9:41 pm   

Joined: Wed Mar 28, 2007 8:31 am
Posts: 4
Nope, sadly I am developing the script as a standalone java program. Sux, but thats life...lol :o


Top
 Profile YIM  
 PostPosted: Tue May 01, 2007 8:39 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Here's a dumb question... What does "${countarray[$index]}" return before this line?

It sounds like when the script gets there, "${countarray[$index]}" is null, and so to the script sees that line literally as "=1"

If you are still interested in pursuing this angle, can you give me some more of the script? Just the array and things involved in making a counting loop maybe?


Top
 Profile YIM  
 PostPosted: Fri May 04, 2007 2:10 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
if you are just trying to populate the array's variable with a number that is reflective to the counter (wow... how confusing is that), then that would be rather easy.

I'm guessing you want something like (in pseudo code):

for i = 1 to 10
do
array(i)=i+1
done

that would be
Code:
index=0
for i in `seq 1 10`
do
let value=i+1
array[${i}]=$value
done


Let me know if I'm barking up the wrong tree :)


Top
 Profile  
 PostPosted: Wed Jun 20, 2007 8:02 am   

Joined: Wed Mar 28, 2007 8:31 am
Posts: 4
jeo wrote:
Here's a dumb question... What does "${countarray[$index]}" return before this line?

It sounds like when the script gets there, "${countarray[$index]}" is null, and so to the script sees that line literally as "=1"

If you are still interested in pursuing this angle, can you give me some more of the script? Just the array and things involved in making a counting loop maybe?


*********
here's the actual code that I am using, it maybe a syntax error, but it is still puzzling me.

LIMIT=70
for ((index=0; index <= LIMIT ; index++))
do
if [ $used1 = ${numbarray[$index]} ]
then
arrayvalue=${countarray[$index]}

echo -e $arrayvalue
arrayvalue=`expr $arrayvalue + $single`
echo -e "arrayvalue after adding 1 ---->" $arrayvalue

${countarray[$index]}=`expr ${countarray[$index]} + $single`
echo -e "count array ---->" ${countarray[$index]}
fi
done
done


Thanks in advance!


Top
 Profile YIM  
 PostPosted: Wed Jun 20, 2007 12:43 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
prezstain wrote:
jeo wrote:
Here's a dumb question... What does "${countarray[$index]}" return before this line?

It sounds like when the script gets there, "${countarray[$index]}" is null, and so to the script sees that line literally as "=1"

If you are still interested in pursuing this angle, can you give me some more of the script? Just the array and things involved in making a counting loop maybe?


*********
here's the actual code that I am using, it maybe a syntax error, but it is still puzzling me.

LIMIT=70
for ((index=0; index <= LIMIT ; index++))
do
if [ $used1 = ${numbarray[$index]} ]
then
arrayvalue=${countarray[$index]}

echo -e $arrayvalue
arrayvalue=`expr $arrayvalue + $single`
echo -e "arrayvalue after adding 1 ---->" $arrayvalue

${countarray[$index]}=`expr ${countarray[$index]} + $single`
echo -e "count array ---->" ${countarray[$index]}
fi
done
done


Thanks in advance!

Code:
index <= LIMIT

should be
Code:
$index <= $LIMIT

I'm not sure about the use of <= I'm almost positive it doesn't work
and that you should use the OR ( || ) instead like...
Code:
$index < $LIMIT || $index = $LIMIT


I havn't really dove into the rest... that's just what I see on the surface. Let me know your findings :)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 2 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