Code Comments
Programming Forum and web based access to our favorite programming groups.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]"|" }
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.