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.
# 8  
Old 04-29-2009
MySQL

Quote:
Originally Posted by Franklin52
Why not? Is this a homework question?
FYI : I am not any college student.

Just out of curiousity i wanted to know if there is option available in shell (to be paticular BASH Shell) that can be used directly to do the above specified thing.

Regards,
14341
# 9  
Old 04-29-2009
Quote:
Originally Posted by devtakh
Please help us understand what do you mean by using an external index. Can you give an example in english algorithm.


cheers,
Devaraj Takhellambam
Consider the following snippets.
Code:
for var in ${num[@]}
do
#do what ever u want
#here var points to num[0],num[1],..num[END] as #loop progresses 
#
#Note that you did't use any variable to point to
#to 0,1,2,...END
done

whereas
Code:
n=${#num}

for ((i=0;i<$n;i++)); do
  echo ":${num[$i]} :${words[$i]}:"
  #here we explicitly declared "$i" to point as index
done

# 10  
Old 04-29-2009
What about this. [untested]

Code:
n=${#num}

for i in 0 .. $n
echo ":${num[$i]} :${words[$i]}:"
done

cheers,
Devaraj Takhellambam
# 11  
Old 04-29-2009
Quote:
Originally Posted by 14341
FYI : I am not any college student.

Just out of curiousity i wanted to know if there is option available in shell (to be paticular BASH Shell) that can be used directly to do the above specified thing.

Regards,
14341
I'm still not following what you want.

You need to access each element of an array so you need to reference the element number some how.

You are asking if you can access 2 arrays at the same time? Each case is showing you can in the same echo statement as array elements are just ${array[element]}

You just echo/change/assign the element of the array you want

Last edited by lavascript; 04-29-2009 at 09:48 AM..
# 12  
Old 04-29-2009
MySQL

Thank you for all the replies.

Right now I am using the following. (using count as index)

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)

count=0
while [ $count -lt ${#num} ]
do
    echo ":${num[$count]}: :${words[$count]}:"
    ((count++))
done

This is working fine.

But i wanted to know the internal execution of shell
i.e if you give
for var in ${num[@]}
It fetches the value of "num[index]" to "var", where index is its internal implementation.
I just wanted to know if there any way to obtain that internal index (using any special keywords/symbols) so that i can directly use it to reference the values of other arrays. (That's why i mentioned arrays of same size)
Excuse me for my bad communication if you still don't understand my question.

Thanks,
14341
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