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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-06-2008, 12:21 PM
|
#1
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387
Rep:
|
awk: Using split to divide string to array. How do I find out the number of elements?
I'm trying to split a string by a delimiter into an array in awk. How do I find out the number of elements in the array?
Thanks,
vxc
|
|
|
02-06-2008, 12:24 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Code:
awk '{print "line = " $0 " record num = " NR " num feilds = " NF}'
edit: corrected code
Last edited by schneidz; 02-06-2008 at 12:27 PM.
|
|
|
02-06-2008, 12:27 PM
|
#3
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387
Original Poster
Rep:
|
Quote:
Originally Posted by schneidz
Code:
awk '{print "line = " $0 " num records = " NR}'
|
Hey,
I meant that I'm splitting a string using split. Like:
According to the awk manual this should split $0 using period as a delimiter and store it in the array a. I want to know how I can find out how many elements are in a.
Last edited by vxc69; 02-06-2008 at 12:28 PM.
|
|
|
02-06-2008, 12:34 PM
|
#4
|
Member
Registered: May 2002
Posts: 964
Rep:
|
awk code snippet
Code:
arrcnt=0
for(i in a) { arrcnt++}
You can put this in a function or use it inline, arrcnt is the number of elements in the array a.
|
|
|
02-06-2008, 12:37 PM
|
#5
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
this should work not sure how efficient it is.
|
|
|
02-06-2008, 12:41 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Or, w/o having tested the performance difference to jim's suggestion ...
Code:
num=asort(a,d)
delete d
Cheers,
Tink
P.S.: Heh ... beat by a few minutes :}
I shouldn't open several posts at a time, really ;}
|
|
|
02-06-2008, 12:50 PM
|
#7
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by Tinkster
Cheers,
Tink
P.S.: Heh ... beat by a few minutes :}
I shouldn't open several posts at a time, really ;}
|
this time, advantage schneidz .
|
|
|
02-06-2008, 12:58 PM
|
#8
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387
Original Poster
Rep:
|
n=asort(a) will work, thanks guys, hope time complexity won't be a prob for large values!
|
|
|
02-06-2008, 11:23 PM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by vxc69
I want to know how I can find out how many elements are in a.
|
Code:
n = split(.., array , ..)
print array[n]
n is the number of elements
|
|
|
02-09-2008, 01:49 PM
|
#10
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
Code:
n = split(.., array , ..)
print array[n]
n is the number of elements
|
Even better. Have a feeling sorting will cause even more issues. Thanks.
|
|
|
All times are GMT -5. The time now is 05:25 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
|
|