How to reference 2 dimensional array in awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reference 2 dimensional array in awk?
# 1  
Old 03-05-2013
How to reference 2 dimensional array in awk?

Hello, all

For a 1-dimensional array, such as
Code:
myarr_1[AA]=1
myarr_1[BB]=2
myarr_1[CC]=3

I know I can write a loop as below to show the array member one by one:
Code:
for (i in myarr_1){print i, myarr_1[i]}

Now, suppose I have a two dimensional array such as:
Code:
myarray_2[1,AA]=1 myarray_2[1,BB]=2
myarray_2[2,AA]=10 myarray_2[2,BB]=20

My questions are:
1.how to write code to print the array member one by one?
2.how to write code to only print "myarray_2[1,AA]", "myarray_2[1,BB]"?
# 2  
Old 03-05-2013
awk does not actually have two-dimensional arrays; it handles two indexes by squeezing them into a single string. As such, your first loop will work but may look weird; the SUBSEP character which it puts between them defaults to a nonprinting ASCII character but can be anything you set it to.

I don't know of a way to iterate over only part of an array. The best I think you can do is iterate over the whole array, and ignore the parts you don't want.

Code:
for(X in ARR) {
        split(X, Q, SUBSEP);
        if(Q[1] != "5") continue;

        print Q[1], Q[2], ARR[X];
}

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-05-2013
You can also do something like this:
Code:
awk '{A[$1,$2]=$3; I[$1]; J[$2]} END{ for(i in I)for(j in J) print i, j, A[i,j]}' file

or for example:
Code:
awk '{A[$1,$2]=$3; J[$2]} END{ for(j in J) print 5, j, A[5,j]}' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-05-2013
Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi Dimensional array

I have an array of names. Each one of the name, has a number represented to it. For example A has an ID 8, B has an ID 2. What I am after is a for loop that when the array is in position 1, a particular variable is set to the value of position 1 in array 2 declare -a arr=("A" "B" "C"... (6 Replies)
Discussion started by: nms
6 Replies

2. Shell Programming and Scripting

Manipulating a list into a two-dimensional array

hi, total newbie to shell scripting and wondering if some of you guru's can give me a hand on a problem I'm trying to solve. The tmplsnr.a file contains LSNR_51526 db1 db2 LSNR_51527 db3 db4 db5 Summary - depending on which db is set, the script will start the relevant listener... (5 Replies)
Discussion started by: mingy10
5 Replies

3. Programming

Return two dimensional array in c++

I am writing matrix multiplication and trying to return a two dimensional array from a function but I keep getting errors. Can someone please help me? here is my code (it is just the skeleton of my program): void main () { ... int *matmultiply (int, int, int, int , int , int ) ... } ... (4 Replies)
Discussion started by: saboture88
4 Replies

4. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

5. Shell Programming and Scripting

Match elements in an AWK multi-dimensional array

Hello, I have two files in the following format; file1: A B C D E F G H I J K L file2: 1 2 3 4 5 6 7 8 9 10 11 12 I have read them both in to multi-dimensional arrays. I need a file that has column 2 of the first file printed out for each column 3 of the second file ie... ... (3 Replies)
Discussion started by: cold_Que
3 Replies

6. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

7. Shell Programming and Scripting

2 dimensional array in unix

I am trying to implementing two dimensinal array in ksh script.Would you pls help me out. I have a large size of file, File contains looks like ID SID VLAUE1 VALUE2 TOTALVALUE 1 a1 01 02 03 1 b1 02 05 07 ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

8. Shell Programming and Scripting

Multi Dimensional array in KSH

Is there any way to use multi dim. array in KSH ? (1 Reply)
Discussion started by: sinpeak
1 Replies

9. Shell Programming and Scripting

Help for record (2 dimensional array.)

I am going to develop a address book using the shell scripting commands without sed, awk, .... I am thinking to apply the concept of 2 dimenstional array. Can I create a two dimensional array for the insertion/updation/deletion of record in unix. If yes then tell me plz or recommend me some... (1 Reply)
Discussion started by: murtaza
1 Replies

10. Shell Programming and Scripting

Reference two dimensional array in Perl sub

I am trying to reference a two dimensional array in a subroutine and can't seem to figure this one out in Perl. Does anybody know? Please enlighten me. #!/usr/bin/perl -w use constant DIM => 4; sub Shift_elements_right{ my (@Input, @Output) = @_; for ($i = 0 ; $i <= DIM ;... (5 Replies)
Discussion started by: photon
5 Replies
Login or Register to Ask a Question