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: Robert Elz <kre%munnari.OZ.AU@localhost>
To: ks%ub.uni-mainz.de@localhost
Cc: gnats-bugs%NetBSD.org@localhost
Subject: Re: bin/53507: awk doesn't properly compare "numeric strings"
Date: Wed, 08 Aug 2018 22:38:02 +0700

     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).
 
   | >-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 ==).
 
   | >-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.
 
 kre
 


Home | Main Index | Thread Index | Old Index