Sponsored Content
Top Forums Shell Programming and Scripting Loop through array of arrays of string with spaces Post 302697193 by kidmanos on Thursday 6th of September 2012 08:38:43 AM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ARRAY_MERGE(3)								 1							    ARRAY_MERGE(3)

array_merge - Merge one or more arrays

SYNOPSIS
array array_merge (array $array1, [array $...]) DESCRIPTION
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. PARAMETERS
o $array1 - Initial array to merge. o $... - Variable list of arrays to merge. RETURN VALUES
Returns the resulting array. EXAMPLES
Example #1 array_merge(3) example <?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?> The above example will output: Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Example #2 Simple array_merge(3) example <?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?> Don't forget that numeric keys will be renumbered! Array ( [0] => data ) If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator: <?php $array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = $array1 + $array2; var_dump($result); ?> The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored. array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" } Example #3 array_merge(3) with non-array types <?php $beginning = 'foo'; $end = array(1 => 'bar'); $result = array_merge((array)$beginning, (array)$end); print_r($result); ?> The above example will output: Array ( [0] => foo [1] => bar ) SEE ALSO
array_merge_recursive(3), array_replace(3), array_combine(3), array operators. PHP Documentation Group ARRAY_MERGE(3)
All times are GMT -4. The time now is 09:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy