Array question | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Array question

UNIX for Dummies Questions & Answers


Tags
echo

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
locoroco locoroco is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 February 2014, 7:50 PM EST
Posts: 127
Thanks: 44
Thanked 3 Times in 3 Posts
Array question

I have attempted to create an array consisting of two items: #0 and #1.
I am able to print the two items corrctly:

Code:
arr=(hello "my name is")
echo ${arr[0]}
hello
echo ${arr[1]}
my name is

However, when I try to run a for loop to print both objects:

Code:
for i in ${arr[*]}
do
echo $i
done

I get:

Code:
hello
my
name
is

If there are two objects, why is the output on four lines? How do I loop on two lines?

My goal is to get:

Code:
hello
my name is

Sponsored Links
    #2  
Old 01-09-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 December 2014, 1:53 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,371
Thanks: 47
Thanked 853 Times in 674 Posts
The problem is the behavior of the "for"-loop in connection with an obvious misunderstanding about what "${arr[*]}" means.

The subscript "*" in an array denotes ALL array elements and it is expanded before the for-loop is executed. Therefore this is what the shell "sees":


Code:
for i in ${arr[*]}            # original line, begin of parsing process
for i in hello my name is     # after expanding the variables

Now, "$i" is assigned one word each pass of the for-loop because this is how this kind of loop behaves. There are two possibilities to correct this:

1. Use a while-loop instead:


Code:
echo ${arr[*]} | while read a ; do
     echo $a
done

2. Instead of using this faulty way of expanding the array count elements:


Code:
i=1
for i in {1..${#arr[*]}} ; do
     echo ${arr[$i]}
done

"${#arrayname[*]}" expands to the number of elements in your array (instead of the whole array itself), in your case: 2.

I hope this helps.

bakunin
Sponsored Links
    #3  
Old 01-09-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 16 December 2014, 11:19 AM EST
Location: Houston, Texas, USA
Posts: 676
Thanks: 0
Thanked 203 Times in 195 Posts
try also:

Code:
arr=(hello "my name is")

echo ${arr[0]}
echo ${arr[1]}

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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
array question sacat Programming 2 01-31-2011 04:32 PM
Help! Yet another check element in array Question alan Shell Programming and Scripting 2 07-29-2010 11:57 AM
Array question ahtat99 Shell Programming and Scripting 4 09-11-2006 09:15 AM
Array question TheCrunge UNIX for Dummies Questions & Answers 2 02-23-2005 05:55 PM
Storage array question ncmurf00 Filesystems, Disks and Memory 1 11-25-2002 04:25 PM



All times are GMT -4. The time now is 04:47 PM.