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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-06-2011, 01:59 PM
|
#1
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Rep: 
|
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.
|
12-06-2011, 02:14 PM
|
#2
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
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.
|
12-07-2011, 08:19 AM
|
#3
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by macemoneta
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?
|
|
|
|
12-07-2011, 11:01 AM
|
#4
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
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.
|
12-09-2011, 07:02 AM
|
#5
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by macemoneta
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!
|
|
|
|
12-11-2011, 09:45 PM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,325
|
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])}'
|
|
|
|
12-12-2011, 02:14 PM
|
#7
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:31 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|