bug-gawk
[Top][All Lists]
Advanced

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

Re: Fwd: gawk: numeric comparison on 'sub()' resulted ${n} vars does not


From: Andrew J. Schorr
Subject: Re: Fwd: gawk: numeric comparison on 'sub()' resulted ${n} vars does not work properly
Date: Sat, 8 Feb 2020 14:25:57 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Sat, Feb 08, 2020 at 12:15:36PM -0700, Stephen Dowdy wrote:
> Andy, thanks for the pointer to the 'gawk' specific 'typeof()' function (mawk 
> doesn't have that).

The typeof function is helpful for understanding what's going on, but is
of course not needed.

> 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?

I believe that the man page is correct and that you are missing something.  A
string cannot be considered a "numeric string" unless it comes from user input
or a certain set of functions.  Here is a relevant snippet from the docs:

https://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html

   The POSIX standard uses the term "numeric string" for input data that
looks numeric.  The '37' in the previous example is a numeric string.
So what is the type of a numeric string?  Answer: numeric.

   The type of a variable is important because the types of two
variables determine how they are compared.  Variable typing follows
these definitions and rules:

   * A numeric constant or the result of a numeric operation has the
     "numeric" attribute.

   * A string constant or the result of a string operation has the
     "string" attribute.

   * Fields, 'getline' input, 'FILENAME', 'ARGV' elements, 'ENVIRON'
     elements, and the elements of an array created by 'match()',
     'split()', and 'patsplit()' that are numeric strings have the
     "strnum" attribute.(2)  Otherwise, they have the "string"
     attribute.  Uninitialized variables also have the "strnum"
     attribute.

   * Attributes propagate across assignments but are not changed by any
     use.

In this particular case, because the "sub" function changed the
value of $1, it removed the strnum attribute such that it is now treated
as a string. The "sub" function is considered to be a string operation that
produces string output.

Regards,
Andy



reply via email to

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