LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 12:21 PM   #1
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

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, 12:24 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
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.
 
Old 02-06-2008, 12:27 PM   #3
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

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 12:28 PM.
 
Old 02-06-2008, 12:34 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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, 12:37 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
this should work
Code:
  n = asort(data)
not sure how efficient it is.
 
Old 02-06-2008, 12:41 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 12:50 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
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, 12:58 PM   #8
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

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, 11:23 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 01:49 PM   #10
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration