For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > splitp() for gawk (Was: Re: awk challenge)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author splitp() for gawk (Was: Re: awk challenge)
Kenny McCormack

2004-11-21, 3:57 am

In article <cnigun$4et$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
(Earlier, I wrote):
>The effect is that if you use both functions, you end up with everything on
>the line, categorized as wheat or chaff. I.e.,
>
>n = split($0,T,/someRE/)
>n1 = splitp($0,T1,/theSameRE/)
>
>You end up with the fields in T[] and the delimiters in T1[].
>
>May I humbly suggest that splitp() would be a nice addition to GAWK?


Having posted this earlier, I have come to think about it and have realized
this can easily be done as an AWK function. In fact, splitp() is really
the same underlying concept as extract(). Here are a couple of funtions:

# splitp() for gawk
function extract(s,re) {
match(s,re)
return substr(s,RSTART,RLENGTH)
}
function splitp(s,A,re, i,t) {
delete A
for (i=0; t = extract(s,re); A[++i] = t)
s = substr(s,RSTART+RLENGTH)
return i
}

And here is a test program:
{ print "Result:",n = splitp($0,A," [A-Z]+ ")
for (i=1; i<=n; i++) print i,"|"A[i]"|" }

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com