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 James

2004-11-19, 3:55 am

The posted code fails when s = " " and r = " +".

Try this:

function match_cols( s, regexp, a, i,first,offset,len )
{ first = 1
while ( match( s, regexp) )
{ if ( RSTART > 1 )
{ RSTART += offset
a[++i] = first
a[++i] = RSTART - 1
}
sub( regexp, "", s )
offset += RLENGTH
first = RSTART + RLENGTH
}
len = length(s) + offset
if ( len >= first )
{ a[++i] = first
a[++i] = len
}
return i
}
Kenny McCormack

2004-11-19, 3:55 am

In article <f8860640.0411181709.7e50215b@posting.google.com>,
William James <w_a_x_man@yahoo.com> wrote:
>The posted code fails when s = " " and r = " +".
>


What's this supposed to do?

>
>function match_cols( s, regexp, a, i,first,offset,len )
>{ first = 1
> while ( match( s, regexp) )
> { if ( RSTART > 1 )
> { RSTART += offset
> a[++i] = first
> a[++i] = RSTART - 1
> }
> sub( regexp, "", s )
> offset += RLENGTH
> first = RSTART + RLENGTH
> }
> len = length(s) + offset
> if ( len >= first )
> { a[++i] = first
> a[++i] = len
> }
> return i
>}



Kenny McCormack

2004-11-22, 8:55 pm

In article <f8860640.0411181709.7e50215b@posting.google.com>,
William James <w_a_x_man@yahoo.com> wrote:
>The posted code fails when s = " " and r = " +".
>


What's this supposed to do?

>
>function match_cols( s, regexp, a, i,first,offset,len )
>{ first = 1
> while ( match( s, regexp) )
> { if ( RSTART > 1 )
> { RSTART += offset
> a[++i] = first
> a[++i] = RSTART - 1
> }
> sub( regexp, "", s )
> offset += RLENGTH
> first = RSTART + RLENGTH
> }
> len = length(s) + offset
> if ( len >= first )
> { a[++i] = first
> a[++i] = len
> }
> return i
>}



William James

2004-11-23, 3:55 am

The posted code fails when s = " " and r = " +".

Try this:

function match_cols( s, regexp, a, i,first,offset,len )
{ first = 1
while ( match( s, regexp) )
{ if ( RSTART > 1 )
{ RSTART += offset
a[++i] = first
a[++i] = RSTART - 1
}
sub( regexp, "", s )
offset += RLENGTH
first = RSTART + RLENGTH
}
len = length(s) + offset
if ( len >= first )
{ a[++i] = first
a[++i] = len
}
return i
}
Sponsored Links







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

Copyright 2008 codecomments.com