Question on iterating array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on iterating array elements
# 1  
Old 05-15-2015
Question on iterating array elements

Hi,

I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values

Code:
$: cat y.ksh
#!/bin/ksh
# set array called nameservers
set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5

# print all name servers
for i in ${nameservers[@]}
do
        echo $i
done
$: ./y.ksh
192.168.1.1
192.168.1.5
202.54.1.5

My test script looks like below:

Code:
#!/bin/ksh
#
#

set -A arrSID
set -A arrVERSION

arrVERSION[1]="Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production"
arrVERSION[2]="Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production"

echo
echo
for i in ${arrVERSION[@]}
do
   echo $i
done
echo
echo

It's output when I run it is as below:

Code:
$: ./x.ksh


Oracle
Database
11g
Enterprise
Edition
Release
11.2.0.4.0
-
64bit
Production
Oracle9i
Enterprise
Edition
Release
9.2.0.8.0
-
64bit
Production

I managed to use the while loop below and manage to get the output that I wanted.

Code:
$: ./z.ksh
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
$: cat z.ksh
#!/bin/ksh
#
#

set -A arrSID
set -A arrVERSION

arrVERSION[1]="Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production"
arrVERSION[2]="Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production"

i=1
while [[ $i -le ${#arrVERSION[@]} ]]
do
   echo ${arrVERSION[$i]}
   (( i=i+1 ))
done

exit 0

I just want to know why the for loop, i.e. the x.ksh, does not work like I expect it to be? I am guessing it is due to the spaces in the string. Is there any way around it. I just thought it is easier to use the for loop instead of the while loop.

Anyway, any feedback much appreciated. Thanks.
# 2  
Old 05-15-2015
You could use the array indices in the for loop:
Code:
for i in ${!arrVERSION[*]}
do
   echo "${arrVERSION[$i]}"
done

# 3  
Old 05-15-2015
Try quoting:
Code:
for i in "${arrVERSION[@]}"
  do echo $i
  done

# 4  
Old 05-15-2015
Quote:
Originally Posted by newbie_01
I just want to know why the for loop, i.e. the x.ksh, does not work like I expect it to be?
The construct "${array[@]}" expands to all elements of this array. Note that also the construct "${array[*]}" is expanded to all the array elements. The difference of these seemingly identical constructs is the used separating character. "${arr[@]}" uses an IFS character, "${arr[*]}" does not.

You will not see a difference in the following call:

Code:
arr[1]="one two three"
arr[2]="four five six"

print - "-------- Using the asterisk:"
for i in ${arr[*]} ; do
     print - $i
done

print - "-------- Using the ampersand:"
for i in ${arr[@]} ; do
     print - $i
done

# ./arrtest.sh:
-------- Using the asterisk:
one
two
three
four
five
six
-------- Using the asterisk:
one
two
three
four
five
six

But once you quote properly (notice the double quotes around the variables to be expanded):

Code:
arr[1]="one two three"
arr[2]="four five six"

print - "-------- Using the asterisk:"
for i in "${arr[*]}" ; do
     print - $i
done

print - "-------- Using the asterisk:"
for i in "${arr[@]}" ; do
     print - $i
done


# ./arrtest.sh 
-------- Using the asterisk:
one two three four five six
-------- Using the asterisk:
one two three
four five six

The reason i sthe way the shell parses its input: in a first step all the variables are expanded, So from the line

Code:
for i in "${arr[@]}" ; do

The shell creates:

Code:
for i in "one two three[IFS-char]four five six" ; do

In a second step this is fed to the (internal) "for"-command, which evaluates this again. At this point " " (blank) and "[IFS]" (which, per default, is a blank too) have no difference and therefore get mixed up: this is why the first example produced each word on a separate line. Once you protect this line (by quoting it) from the shells interpretation it gets read correctly - at least with the "@" because the "*" now has another (and in your case equally unintended) meaning.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

5. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

6. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

7. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

8. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

9. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies
Login or Register to Ask a Question