awk small discussion on array

Hi...all

I want to pass array name for some function using loop anyone is having idea ?

here is scenario

echo | awk ' 
        BEGIN{
            A1[1]="foo1"
            A2[1]="foo2"
            A3[1]="foo3"
             }

        function test1(a,b,c){
                    print a,b,c
                     }
        function test2(a,b,c){
                    print b[1]
                     }
        {
        for(i=1;i<=4;i++)
                test1(i,"A"i,"B"i+1)
                test2(i,"A"i,"B"i+1)
        }'

Resulting

sh test.sh 
1 A1 B2
2 A2 B3
3 A3 B4
4 A4 B5
awk: cmd. line:11: (FILENAME=- FNR=1) fatal: attempt to use scalar parameter `b' as an array

someone help me..I don't know whether syntax is legal or not...waiting for quick solution....from unix experts...

You are not passing any array, just a string is being passed and thats why the error.

Try like this:

 

                test1(i,"A"i,"B"i+1);
                split("hi:aa:ab",c,":");
                test2(i,c,"B"i+1);

Guru.

Thanks for reply....

in

BIGIN 

block I have defined array

A1[1],A2[1]..

here I want to send array name to function.In first function test1 I am just printing only arguments thats is

print a,b,c

what you said is

array c contains index 1....n , that is c[1],c[2]...c[n]

but in my case I have

array c1[1]..c2[1]...c5[1]....cn[1]

I think something going wrong while calling function test2..here my interest is to use loop index along with string A to call function. as you know calling array n number of times manually is laborious like below

test2(i,A1,"B"i+1)
test2(i,A2,"B"i+1)
test2(i,A3,"B"i+1)
test2(i,A4,"B"i+1)

This is something which can be done with eval in bash I think.
What is your original requirement anyways? May be we can sort that out somehow.

--ahamed

You mean it's impossible in awk ?? what I posted is my requirement..I am loosing my hopes...

You have not posted your requirement, only the manner you are determined to solve said requirement... This is a very common failure mode where people get trapped working on the very first solution which comes to mind to the point they cannot imagine other solutions.

If you could post your actual goal, there may be a much simpler way.

3 Likes

Thanks Corona688 for reply. I am just playing with array.. first I tried like this

echo | awk ' 
        BEGIN{
            A[0,1]="foo1"
            A[0,2]="foo2"
            A[0,3]="foo3"
             }

        function test2(a,b,c){
                    print b[0,a]
                     }
        {
        for(i=1;i<=4;i++)
                test2(i,A,"B"i+1)
        }'

got result like this as expected

sh test.sh 
foo1
foo2
foo3

then I tried in another way which I posted, please note which I posted is not classroom homework, I just wanted to know,why I am getting fatal: attempt to use scalar parameter `b' as an array, I was keep on searching solution on google, I couldn't find it, so I posted. I request moderator please close this post if there is no solution.

Because you did not pass the array A1 into it, you passed the string "A1" into it. Variables do not work that way.

Yeah..I agree its string..any alternate solution ???

My general suggestion would be "don't do that"... Almost no language allows this, and it's still ugly in those which can. It's simple to organize your data in any linear structure so it's unnecessary, too.

The usual alternative for these is arrays, especially associative arrays or multidimensional arrays instead of variable variable names, not as well as. A[0,...] instead of A0[...] essentially.

Thank you Corona688.