How to access the elements of two arrays with a single loop using the inbuilt index.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to access the elements of two arrays with a single loop using the inbuilt index.
# 1  
Old 04-29-2009
MySQL How to access the elements of two arrays with a single loop using the inbuilt index.

Hi all,
I wanted to access two arrays (of same size) using one for loop.

Ex:

Code:
#!/bin/bash

declare -a num
declare -a words

num=(1 2 3 4 5 6 7)
words=(one two three four five six seven)

for num in ${num[@]}
do
        echo ":$num: :${words[$in]}:"
done

Required Output:
Code:
:1: :one:
:2: :two:
:3: :three:
:4: :four:
:5: :five:
:6: :six:
:7: :seven:

*** I don't want to use an external index.

Please help me.

Thanks,
14341

Last edited by Franklin52; 04-29-2009 at 06:33 AM.. Reason: adding code tags
# 2  
Old 04-29-2009
Couple of things.

in is not a variable in the for num in loop.
I wouldn't use num in the loop as you have it declared for an array. Basically accessing $num will access element 0 of the array.

What about :-

Code:
#!/bin/bash

declare -a num
declare -a words

num=(1 2 3 4 5 6 7)
words=(one two three four five six seven)

for var in ${num[@]}
do
        echo ":${num[$var]}: :${words[$var]}:"
done

That's without any testing. Also you will hit a problem as 1 and one will be assigned to element 0 of the array and not element 1.

I've just got a meeting but will check back and test later. It may get you started though
# 3  
Old 04-29-2009
MySQL

Thanks for the quick reply.

You are actually printng
:num[num[0]]: :word[num[0]]:
:num[num[1]]: :word[num[1]]:
...
:num[num[6]]: :word[num[6]]:

I have chosen
num=(1 2 3 4 5 6 7)
But if i had
num=(eleven tweleve thirteen fourteen...seventeen)

The above script does't work.

I wanted just know if there is any way we could access the elements of multiple arrays (of same size)
simultaneously.

Thank You,
14341
# 4  
Old 04-29-2009
With bash you can use a for loop like:

Code:
#!/bin/bash

declare -a num
declare -a words

num=(1 2 3 4 5 6 7)
words=(one two three four five six seven)

n=${#num}

for ((i=0;i<$n;i++)); do
  echo ":${num[$i]} :${words[$i]}:"
done

# 5  
Old 04-29-2009
Thanks for the reply.

But in the initial post itself i pointed out that i don't want to use an external index.

Regards,
14341
# 6  
Old 04-29-2009
Quote:
Originally Posted by 14341
Thanks for the reply.

But in the initial post itself i pointed out that i don't want to use an external index.

Regards,
14341
Please help us understand what do you mean by using an external index. Can you give an example in english algorithm.


cheers,
Devaraj Takhellambam
# 7  
Old 04-29-2009
Quote:
Originally Posted by 14341
Thanks for the reply.

But in the initial post itself i pointed out that i don't want to use an external index.

Regards,
14341
Why not? Is this a homework question?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply elements of 2 arrays together into another array

So I need to Write an array processing program using a Linux shell programming language to perform the following. Load array X of 20 numbers from an input file X. Load array Y of 20 numbers from an input file Y. Compute array Z by multiply Xi * Yi then compute the square-root of this... (2 Replies)
Discussion started by: sarapham409
2 Replies

2. Shell Programming and Scripting

Sum elements of 2 arrays excluding labels

I'm looking for an efficient way to sum elements from 2 arrays using AWK and preserve header as well as sample names in the output array. I have Ubuntu 16.04 LTS. For example; ARRAY 1 SAMPLE DERIVED ANCESTRAL Sample1 14352 0 Sample2 14352 0 Sample3 14352 0 Sample4 ... (8 Replies)
Discussion started by: Geneanalyst
8 Replies

3. Shell Programming and Scripting

Perl:: Arrays w/ UInt64.max>index>Int64

Where is the delete or remove post option? (1 Reply)
Discussion started by: f77hack
1 Replies

4. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

5. Shell Programming and Scripting

file index operation in loop

i have one file which contains data like the below DE_CODE|AXXANY|APP_NAME|TELCO|LOC|NY DE_CODE|AXXATX|APP_NAME|TELCO|LOC|TX DE_CODE|AXXABT|APP_NAME|TELCO|LOC|BT DE_CODE|AXXANJ|APP_NAME|TELCO|LOC|NJ DE_CODE|AXXANJwt|APP_NAME|TELCO|LOC|WT am going use a for loop or whole loop which will... (3 Replies)
Discussion started by: mail2sant
3 Replies

6. Shell Programming and Scripting

Iterating through two arrays in a single loop

Hey everyone. Is it possible to use two arrays in a loop? Basically what I am trying to do is iterate through the elements in an array, and, based on a condition, use the current position in the array to point to that index in the array. Here's the loop structure I'm looking for: ... (1 Reply)
Discussion started by: msarro
1 Replies

7. Shell Programming and Scripting

PHP arrays as array elements

PHP question...I posted this on the Web Development forum, but maybe this is a better place! 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... (3 Replies)
Discussion started by: JerryHone
3 Replies

8. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

9. UNIX for Advanced & Expert Users

which access right should set in my webpage index.html ?

I have a webpage, http://my.dns.com/~zp523/index.html, I want all people to have read and execute privileges. I want to extend it with execute privilege. Which command should be used in chmod? is it only give read(r) & execute(x) parameter in 'chmod ??? index.html' thk a lot!! (1 Reply)
Discussion started by: zp523444
1 Replies
Login or Register to Ask a Question