For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > 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 Re: awk challenge
A Ferenstein

2004-11-16, 6:50 pm

Either put up or shut up. (I believe this is a very usefulness of this
function is self-evident - specification of table can be defined at
beginning by only specifying the delimiter strings.)

Here's my version, "matchCol(s, r, bA)", where s is the source line, r is
regexp spefying delimiter b/w columns; bA[1..] is resultant array of
columns.

--------------

function matchCol(s, r, bA) {
bL=0
if (match(s, r)) {if (RSTART!=1) bA[++bL]= 1}
else bA[++bL]= 1
bL= matchCol2(s, r, bA, 0, bL)
if (bA[bL-1]>bA[bL]) bL-= 2 # if end is regexp
return bL
}

function matchCol2(s, r, bA, posn, bL) {
print "incoming s=" s", bL="bL", bA[bL]="bA[bL]
if (match(s, r)) {
if (bL) bA[++bL]= posn+RSTART-1 # before current regexp
if (bA[bL]+1==bA[bL-1]) bL-= 2 # if regexp is same as last
bA[++bL]= posn+RSTART+RLENGTH # after current regexp
bL= matchCol2(substr(s, RSTART+RLENGTH), r, bA, bA[bL]-1, bL)
}
else bA[++bL]= posn+length(s) # before last regexp
return bL
}


William Park

2004-11-16, 6:50 pm

A Ferenstein <epaalx@hotmail.com> wrote:
> Either put up or shut up. (I believe this is a very usefulness of this
> function is self-evident - specification of table can be defined at
> beginning by only specifying the delimiter strings.)
>
> Here's my version, "matchCol(s, r, bA)", where s is the source line, r is
> regexp spefying delimiter b/w columns; bA[1..] is resultant array of
> columns.
>
> --------------
>
> function matchCol(s, r, bA) {
> bL=0
> if (match(s, r)) {if (RSTART!=1) bA[++bL]= 1}
> else bA[++bL]= 1
> bL= matchCol2(s, r, bA, 0, bL)
> if (bA[bL-1]>bA[bL]) bL-= 2 # if end is regexp
> return bL
> }
>
> function matchCol2(s, r, bA, posn, bL) {
> print "incoming s=" s", bL="bL", bA[bL]="bA[bL]
> if (match(s, r)) {
> if (bL) bA[++bL]= posn+RSTART-1 # before current regexp
> if (bA[bL]+1==bA[bL-1]) bL-= 2 # if regexp is same as last
> bA[++bL]= posn+RSTART+RLENGTH # after current regexp
> bL= matchCol2(substr(s, RSTART+RLENGTH), r, bA, bA[bL]-1, bL)
> }
> else bA[++bL]= posn+length(s) # before last regexp
> return bL
> }


What does this supposed to do? Your solution doesn't match example you
posted. The count is wrong. Repost.
Sponsored Links







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

Copyright 2008 codecomments.com