LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-06-2011, 01:59 PM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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!!!

Last edited by cristalp; 12-06-2011 at 02:03 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-06-2011, 02:14 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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];
        }
    }'
 
2 members found this post helpful.
Old 12-07-2011, 08:19 AM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by macemoneta View Post
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?
 
Old 12-07-2011, 11:01 AM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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.
 
1 members found this post helpful.
Old 12-09-2011, 07:02 AM   #5
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by macemoneta View Post
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!
 
Old 12-11-2011, 09:45 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,476

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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])}'
 
Old 12-12-2011, 02:14 PM   #7
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
  


Reply

Tags
array, awk, loop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested loop questions and using a counter to increment array output aSingularity Programming 24 01-19-2012 02:06 PM
Is there a way to print a range of a char array without a for loop? trist007 Programming 3 11-07-2010 09:15 AM
assigning column name to awk print in a loop bashprog Programming 5 09-29-2010 01:54 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM


All times are GMT -5. The time now is 12:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration