LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-06-2008, 11:21 AM   #1
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385

Rep: Reputation: 33
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
 
Old 02-06-2008, 11:24 AM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,847

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Code:
awk '{print "line = " $0 " record num = " NR  " num feilds = " NF}'
edit: corrected code

Last edited by schneidz; 02-06-2008 at 11:27 AM.
 
Old 02-06-2008, 11:27 AM   #3
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by schneidz View Post
Code:
awk '{print "line = " $0 " num records = " NR}'

Hey,

I meant that I'm splitting a string using split. Like:
Code:
split($0, a, ".")
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 11:28 AM.
 
Old 02-06-2008, 11:34 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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.
 
Old 02-06-2008, 11:37 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,847

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
this should work
Code:
  n = asort(data)
not sure how efficient it is.
 
Old 02-06-2008, 11:41 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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 ;}
 
Old 02-06-2008, 11:50 AM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,847

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Quote:
Originally Posted by Tinkster View Post
Cheers,
Tink

P.S.: Heh ... beat by a few minutes :}
I shouldn't open several posts at a time, really ;}
this time, advantage schneidz .
 
Old 02-06-2008, 11:58 AM   #8
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385

Original Poster
Rep: Reputation: 33
n=asort(a) will work, thanks guys, hope time complexity won't be a prob for large values!
 
Old 02-06-2008, 10:23 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by vxc69 View Post
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
 
Old 02-09-2008, 12:49 PM   #10
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385

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


Reply


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
c: find out if a string represents a number? kpachopoulos Programming 2 08-15-2006 02:57 PM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 12:02 AM
Divide up lines with string delimiter elmu Programming 3 10-07-2005 08:48 AM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM


All times are GMT -5. The time now is 06:33 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