NetBSD-Bugs archive

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

Re: bin/53507: awk doesn't properly compare "numeric strings"



The following reply was made to PR bin/53507; it has been noted by GNATS.

From: "K. Schreiner" <ks%ub.uni-mainz.de@localhost>
To: Robert Elz <kre%munnari.OZ.AU@localhost>
Cc: <gnats-bugs%NetBSD.org@localhost>
Subject: Re: bin/53507: awk doesn't properly compare "numeric strings"
Date: Wed, 8 Aug 2018 20:43:47 +0200

 Hi,
 
 On Wed, Aug 08, 2018 at 10:38:02PM +0700, Robert Elz wrote:
 >     Date:        Wed, 8 Aug 2018 16:58:45 +0200
 >     From:        "K. Schreiner" <ks%ub.uni-mainz.de@localhost>
 >     Message-ID:  <20180808145845.GD5094%imac7.ub.uni-mainz.de@localhost>
 > 
 >   | >-2: awk 'BEGIN { max="10"+0 ; for (i=0; i<max; i++) {print i}; exit}' | wc -l
 >   |       10
 >   | ok, if you want nummeric, add 0
 > 
 > Yes, that's never been in doubt (though it should also be possible to
 > just do +"10" (unary plus is supposed to produce a numeric result)
 > but our awk has what is a bug I believe, and just treats unary + as
 > a no-op, leaving the arg type unchanged.  All the regular arithmetic
 > operators produce numeric results (so -0 would work just as well,
 > but that isn't the idiom, +0 is).
 ok, +0 was what I found in the manuals.
  
 >   | >-4: awk 'BEGIN { max="80" ; for (i=0; i<max; i++) {print i}; exit}' | wc -l
 >   |        9
 >   | hm, why 9? Maybe counts 0 to 8?
 >   |
 >   | >-5: awk 'BEGIN { max="90" ; for (i=0; i<max; i++) {print i}; exit}' | wc -l
 >   |       90
 >   | And now? 90 is ok - or not?
 > 
 > These are because when max is a string, the comparison is a string
 > comparison, and "9" > "80" (just like "b" > "az") but when max="90"
 > i stays < max (even as a string) until i reaches 90 (when they are ==).
 Ah ok, got it! _string_ (comparison) is the magic word!
 
 >   | >-269: which awk
 >   | /usr/xpg4/bin/awk
 >   | >-270: awk 'BEGIN { max="10" ; for (i=0; i<max; i++) {print i}; exit}' | wc -l
 >   |       10
 > 
 > That's broken.
 At least "normal" awk is ok on Slowlaris. ;-)
 
 Thanks a lot for the very competent and patient explanation!
 
 Kurt
 


Home | Main Index | Thread Index | Old Index