|
From: | Stephen Dowdy |
Subject: | Re: Fwd: gawk: numeric comparison on 'sub()' resulted ${n} vars does not work properly |
Date: | Sat, 8 Feb 2020 12:15:36 -0700 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 |
On 2/8/20 11:55 AM, Andrew J. Schorr wrote:
The problem you're encountering is that the call to sub changes the character of $1 from strnum to string. For example: bash-4.2$ echo 55 | ./gawk '{print typeof($1); sub("5","",$1); print typeof($1); if ($1>40) printf("[%s][%d]\n",$1,$1)} ' strnum string [5][5] So you're getting a string comparison. The simple fix is to force a numeric comparision, like so: bash-4.2$ echo 55 | ./gawk '{print typeof($1); sub("5","",$1); print typeof($1); if ($1+0>40) printf("[%s][%d]\n",$1,$1)} ' strnum string
Andy, thanks for the pointer to the 'gawk' specific 'typeof()' function (mawk doesn't have that). Here's something from the 'gawk' man page, however: [ Variable Typing And Conversion ] ... When a string must be converted to a number, the conversion is accomplished using strtod(3) ... If one value is numeric and the other has a string value that is a "numeric string," then comparisons are also done numerically. You say, "i would argue that THIS condition holds true for my comparison." I would argue that my initial test fits the second part of the quote above. it may be a string type, but it contains a "numeric" value. So, a numeric test should have been performed. (from my reading) Am i still missing something? Does the man page require correction? thanks, --stephen
[Prev in Thread] | Current Thread | [Next in Thread] |