Loop through array of arrays of string with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through array of arrays of string with spaces
# 1  
Old 09-06-2012
Loop through array of arrays of string with spaces

Hi

I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of the array are seen as 1 item if I leave IFS unchanged here's some sample code:

Code:
#! /bin/sh
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
   eval arrayz=\${$high_item[@]}
   #IFS=$'\n'
   for item in $arrayz
   do
      echo $item
   done
done

Output:
AA
QQ
BB
LL
CC
DD
EE
FF

As you can see the elements "AA QQ" and "BB LL" have been split.

If I uncomment the line that sets IFS to \n I get the following:

AA QQ BB LL
CC DD
EE FF

Now "AA QQ" and "BB LL" are concatenated!

Is there anyway I can preserve these elements just as they original are...I need the output to look like that:

AA QQ
BB LL
CC DD
EE FF

Any ideas?
# 2  
Old 09-06-2012
Try this..Smilie

Code:
#! /bin/sh
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
   eval arrayz=\${$high_item[@]}
   #IFS=$'\n'
   for item in "$arrayz"
   do
      echo $item
   done
done

# 3  
Old 09-06-2012
hi pamu

tried adding quotes but the strings remain concatenated

AA QQ BB LL
CC DD
EE FF
# 4  
Old 09-06-2012
Like this?
Code:
#! /bin/sh
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
   eval 'for i in "${'$high_item'[@]}";do echo "$i";done'
done

producing
Code:
AA  QQ
BB  LL
CC
DD
EE
FF

# 5  
Old 09-06-2012
Quote:
Originally Posted by kidmanos
I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string.
Code:
#! /bin/sh
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
   eval arrayz=\${$high_item[@]}
   #IFS=$'\n'
   for item in $arrayz
   do
      echo $item
   done
done

... <snip> ...

Any ideas?
Yes. I would simplify the approach. If you're going to hardcode the names of the arrays, then you lose nothing by hardcoding the expansion of those arrays.

Code:
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=("${low1[@]}" "${low2[@]}" "${low3[@]}")

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

A couple other suggestions:
1. On many systems, /bin/sh does not support arrays. Nor does it support that $'\n' string syntax. If a script depends on bash (or other non-/bin/sh shell), it's a good idea to state it explicitly at the start of the script. It's preferable to fail loudly (with an interpreter not found message) than to chug along with syntax errors (or worse, silent errors) while using the wrong interpreter.
2. echo "$data" can be problematic if $data ever resembles a command option. Better to use printf '%s\n' "$data".

Regards,
Alister

Last edited by alister; 09-06-2012 at 12:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Calling an array of arrays in C.

Corona688 was great in helping me learn how to create arrays that hold other two dimensional array here. Unfortunately I didn't think ask about how to implement or call them. Basically, I'm trying to call an array of two-dimensional arrays like this: declaration: int (*side_one) = { { white_l1,... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

Perl : Arrays : where are these spaces coming from

Example data The sub and array in question sub parse_status { my $status_info = shift; my @info; push @info,"$1\n" if $status_info =~ /(System State : +)/; push @info, "System_Uptime : $1\n" if $status_info =~ /Uptime:.+?up (.+?), load/; push @info, "$1\n" if $status_info =~... (2 Replies)
Discussion started by: popeye
2 Replies

5. Shell Programming and Scripting

using arrays and also help with array.contains functionality

here is what i have... i=1 while read line do if grep -i-q "create procedure"<<<$line then startline="$line" endline="blahblah" Get procedure name into a variable named procName procName="procedure name is stored" do some... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

array and string with for-loop and grep

Hello together, first of all, iam really a beginner in Shellskripting and i need some help please. Following Task i try to finished: vmtoolsd --cmd 'info-get guestinfo.ovfEnv' > vmt In the file vmt are some strings that iam searching for. For that i try to create a array like: ... (2 Replies)
Discussion started by: dreipapier
2 Replies

7. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

8. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

9. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

10. Shell Programming and Scripting

SQL Data with Spaces into Arrays

Hi Simple thing has been driving me nuts. I have used the following code is ksh scripts to get data from Oracle table and then display it, allowing user to select one of the data options returned... REP_DATA=`sqlplus -s ${WMSDB} <<EOF SET SERVEROUTPUT ON FEEDBACK OFF... (0 Replies)
Discussion started by: mikem22
0 Replies
Login or Register to Ask a Question