LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   AWK; print array in loop no output (http://www.linuxquestions.org/questions/programming-9/awk%3B-print-array-in-loop-no-output-917437/)

cristalp 12-06-2011 01:59 PM

AWK; print array in loop no output
 
Dear all,

I have tried code:
Code:

awk 'BEGIN {uk[1]=3333;uk[2]=5555;uk[3]=6666;a[1]=0;a[2]=1;a[3]=2;for(i=1;i<=3;i++) mith[i] = sprintf("%2.1f  %s", a[i], uk[i]);print mith[i]}'
But I do not have any output from this code. I tried that if I do not use loop, I can get output. So, what's wrong with the loop and how could I revise it to see the output? (I still need to use the loop to simplify my problem. The array in my real problem contains too many elements)

Thanks!!!

macemoneta 12-06-2011 02:14 PM

If you properly format the code, it's easy to see that you are missing a pair of braces.

Code:

awk 'BEGIN {
        uk[1]=3333;
        uk[2]=5555;
        uk[3]=6666;
        a[1]=0;
        a[2]=1;
        a[3]=2;
        for(i=1;i<=3;i++) {
          mith[i] = sprintf("%2.1f  %s", a[i], uk[i]);
          print mith[i];
        }
    }'


cristalp 12-07-2011 08:19 AM

Quote:

Originally Posted by macemoneta (Post 4543687)
If you properly format the code, it's easy to see that you are missing a pair of braces.

Code:

awk 'BEGIN {
        uk[1]=3333;
        uk[2]=5555;
        uk[3]=6666;
        a[1]=0;
        a[2]=1;
        a[3]=2;
        for(i=1;i<=3;i++) {
          mith[i] = sprintf("%2.1f  %s", a[i], uk[i]);
          print mith[i];
        }
    }'


Thanks! But isn't that for for loop in awk, I do not necessary to have braces for statement? Am I wrong?

macemoneta 12-07-2011 11:01 AM

You don't need braces, if you only have a single statement for the 'for' loop to execute. With multiple statements, it's the only way to know when the 'for' loop ends.

cristalp 12-09-2011 07:02 AM

Quote:

Originally Posted by macemoneta (Post 4544356)
You don't need braces, if you only have a single statement for the 'for' loop to execute. With multiple statements, it's the only way to know when the 'for' loop ends.

Sounds reasonable. Thanks!

grail 12-11-2011 09:45 PM

Personally I would query the need for the extra print at all? Unless you intend to do other calculations / commands
in the for loop?
Code:

awk 'BEGIN {uk[1]=3333;uk[2]=5555;uk[3]=6666;a[1]=0;a[2]=1;a[3]=2;for(i=1;i<=3;i++) printf("%2.1f  %s\n", a[i], uk[i])}'

cristalp 12-12-2011 02:14 PM

Quote:

Originally Posted by grail (Post 4547590)
Personally I would query the need for the extra print at all? Unless you intend to do other calculations / commands
in the for loop?
Code:

awk 'BEGIN {uk[1]=3333;uk[2]=5555;uk[3]=6666;a[1]=0;a[2]=1;a[3]=2;for(i=1;i<=3;i++) printf("%2.1f  %s\n", a[i], uk[i])}'

Hi Grail,

Actually, this is just a simplification for my script. Indeed, I need the array do some other calculations. Thanks anyway for your kind advice.


All times are GMT -5. The time now is 09:21 PM.