Looping through arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through arrays
# 1  
Old 04-27-2013
Looping through arrays

i just started learning arrays and found this example on the net:

Code:
for (( i = 0 ; i < ${#names[@]} ; i++ ))
do
  echo ${names[$i]}
   done

However, even though I can
Code:
echo ${#names[@]}

I am unable to get the increment to work. I have tried eliminating spaces and changing brackets and nothing seems to work. I am using the bash shell. I would really like to get this to work so I can understand more about arrays. I am getting the message:

. syntax error at line 1: `(' unexpected

Anybody have any ideas on how to make this work? They are greatly appreciated.
# 2  
Old 04-27-2013
I tried your loop, and it worked fine for me (GNU bash, version 4.2.37). What bash version do you use?
# 3  
Old 04-27-2013
I think you are using an early bash shell (version 3) or other shells like ksh. Meaning: that example will not work in your environment.
# 4  
Old 04-27-2013
If so try:

Code:
cnt=0
while [ $cnt -lt ${#names[*]} ]
do
      echo "${names[$cnt]}
      cnt=$(( $cnt + 1 ))
done

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 04-27-2013
Thanks!

This is the version:

[CODE] bash --version[CODE]
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.
This looks old!Smilie

---------- Post updated at 02:12 PM ---------- Previous update was at 01:59 PM ----------

That worked! At first I still got an error, but it was due to my not ending the quotes. It is all working now, thanks again Jim.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies

2. Programming

Looping through multiple arrays in C.

Not sure if this is possible, but I've tried this about a thousand ways now. I am making something with a lot of arrays. I thought I could put the array names into a separate array and then loop through them to call all of their elements. This is the best I've got so far: #include <stdio.h>... (4 Replies)
Discussion started by: Azrael
4 Replies

3. Shell Programming and Scripting

Looping

Hey guys, so I am trying to do a loop script that will go through each folder (no gui so just each domain has a folder) and grab out the databases being used on that domain. I know I would use mysql -e "show databases where not 'information_schema';" once in each directory to pull the actual... (3 Replies)
Discussion started by: dough
3 Replies

4. Shell Programming and Scripting

Looping

Hi evryone i need a help . i have a file xcv.the content is : accelerate i want a script which will run 1000 times in loop and changing the value to accelerate to acceler in 1st loop and in 2nd loop it will be again accelerate and so on . (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

7. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

8. Shell Programming and Scripting

help on looping using if/for or while

Hello, where can I get usefull information on the use of looping with for , if and while with extensive examples. Also use of variables in scripts (1 Reply)
Discussion started by: sam4now
1 Replies

9. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

10. Shell Programming and Scripting

looping

Hi I have around 100 users in sun server and have default home directory in /usr/home/<username> I want to clean their home directory time to time to make free space on root, as users generate many output files during usage of application. My idea is, generate a file with following command... (4 Replies)
Discussion started by: ishir
4 Replies
Login or Register to Ask a Question