2018-08-08 16:26:25 +0700, Robert Elz:
>     Date:        Wed, 8 Aug 2018 09:28:57 +0100
>     From:        Geoff Clare <[email protected]>
>     Message-ID:  <[email protected]>
> 
>   | No it can't.
> 
> Thanks, that's sort of what we thought, but the script exists, and
> rather than blindly just "fix" it we wanted to be sure.
> 
> It "works" as it is, I believe, puerly because the "numbers" it is
> comparing all are the same order of magnitude (have the same
> number of digits) so a string comparison gives the same answer
> as a numeric one (which is what it should be doing) would.
[...]

By the way, you can also convert a string to numeric with:

 if (x+0 < y+0)

It also covers the case where the content of x is from one of
those sources that can be considered as numeric strings (like
-v, $1...) but are not considered as such because they have
trailing garbage.

For instance:

   awk -v 'x=10$' -v 'y=2$' 'BEGIN{print x < y}'

would print 1.

   awk -v 'x=10$' -v 'y=2$' 'BEGIN{print 0+x < 0+y}'

would print 0 as x and y have been converted to 10 and 2
numbers.

BTW, some people use:

   awk -v 'x=10$' -v 'y=2$' 'BEGIN{print +x < +y}'

But that's not portable. In historical implementations, the
unary + is a no-op and doesn't force conversion to number.
That's a conformance bug. I did report it to bwk, but not to
BSDs at the time. I don't know if they're still affected, you
may want to check on NetBSD.

It was discussed here a few years ago:
http://thread.gmane.org/gmane.comp.standards.posix.austin.general/11391

-- 
Stephane

Reply via email to