bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FW: SPLIT function changed?


From: Aharon Robbins
Subject: Re: FW: SPLIT function changed?
Date: Sun, 29 Apr 2001 10:24:54 +0300

Greetings.

As Darrell pointed out, things are working as they're supposed to.
The order of access for a:

        for (i in ff) ...

loop is undefined.  The proper usage for split is:

        n = split($0, ff, ",")
        for (i = 1; i <= n; i++)
                print i, ff[i]  # or do whatever else you want.

Thanks,

Arnold

> From: "Bill Brown" <address@hidden>
> To: <address@hidden>
> Cc: <address@hidden>
> Subject: FW: SPLIT function changed?
> Date: Fri, 27 Apr 2001 11:14:22 -0400
>
> I found these email addresses from GAWK. I am running WIN98 2nd 4.10.2222A.
>
> Seems like I use the SPLIT function in several AWK files and they're all
> broke. Help????
>
> -----Original Message-----
> From: Bill Brown [mailto:address@hidden
> Sent: Tuesday, April 24, 2001 1:31 PM
> To: address@hidden
> Subject: SPLIT function changed?
>
> I have a data extraction AWK program dated 5/15/2000 that has been working
> (It's in a ZIP file for safe keeping) I recently ran it with surprising
> results for the SPLIT function.
>
> I expect SPLIT($0, ff, ",") to split $0 (which is CSV format) into tokens
> with the first token in ff[1], the second in ff[2], etc. Not exactly.
> Consider:
>       N=split($0, ff, ",")
>       for (i in ff) print i, ff[i]
>
> When $0 has more than three tokens the order is 4, 5, ... N, 1, 2, 3. I have
> a test program that exhibits this behavior. See attached test.awk and
> test.txt -- test.txt is the output from
>       gawk -f test.awk > test.txt
>
> A work around seems to be
>       j=1
>       while (j in ff) { j++ }
>       for (i=1; i<j; i++) print i, ff[i]
>
> Any explanations?
>
> ------=_NextPart_000_0000_01C0CF0B.35D64A80
> Content-Type: text/plain;
>       name="test.txt"
> Content-Transfer-Encoding: 7bit
> Content-Disposition: attachment;
>       filename="test.txt"
>
> 8  1 this, 2 is, 3 a, 4 test, 5 of, 6 splitting, 7 a, 8 phrase
> for i in tt --->
>     4  4 test
>     5  5 of
>     6  6 splitting
>     7  7 a
>     8  8 phrase
>     1  1 this
>     2  2 is
>     3  3 a
> for (i=1 ... 
>     1  1 this
>     2  2 is
>     3  3 a
>     4  4 test
>     5  5 of
>     6  6 splitting
>     7  7 a
>     8  8 phrase
>
> ------=_NextPart_000_0000_01C0CF0B.35D64A80
> Content-Type: application/octet-stream;
>       name="TEST.AWK"
> Content-Transfer-Encoding: 7bit
> Content-Disposition: attachment;
>       filename="TEST.AWK"
>
> #--- problem started when an old data extraction program got sick
> #--- No, I did not change it - was dated 1999
> BEGIN{
>     T = " 1 this, 2 is, 3 a, 4 test, 5 of, 6 splitting, 7 a, 8 phrase"
>     nn = split(T, tt, ",")
>     print nn, T
>
> #--- here I expect the split to maintain order - IT DOES NOT!
>
>     print "for i in tt --->"
>     for (i in tt) print "   ", i, tt[i]
>
> #-- seems to be a work around but why???
>
>     j=1
>     while (j in tt) {j++}
>     print "for (i=1 ... "
>     for (i=1; i<j; i++) print "   ", i, tt[i]
>
> }
>
> ------=_NextPart_000_0000_01C0CF0B.35D64A80--



reply via email to

[Prev in Thread] Current Thread [Next in Thread]