bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] string misinterpretation


From: Davide Brini
Subject: Re: [bug-gawk] string misinterpretation
Date: Tue, 30 Apr 2013 10:55:51 +0200

On Tue, 30 Apr 2013 10:43:14 +0200, Davide Brini <address@hidden> wrote:

> On Mon, 29 Apr 2013 15:20:37 -0700, "Love, Mr. Gary, Contractor, Code
> 7542" <address@hidden> wrote:
> 
> > Developers,
> > 
> > I encountered a problem when a value is extracted from a field using the
> > substr function and is then used in a conditional numeric test.  The
> > test fails unless I multiple the substr value by 1.0 because the parser
> > thinks the value is a string.
> > 
> > The attached code and data file demonstrate the problem. The workaround
> > for the problem is to change line 5 From:
> >                          incrm=substr($0,191,14)
> > To:
> >                          incrm=1.0*substr($0,191,14)
> 
> It works for me with gawk 4.0.2 without having to change anything. (btw,
> the usual way to force numeric interpretation is adding 0, but I guess
> multiplying by 1 also works).

I spoke too soon. I thought you were getting an error during execution, but
now I see what you are referring to. What you see is expected behavior,
since substr() returns a string and thus has to be explicitly converted to
a number.

See this section of the manual for more information:

http://www.gnu.org/software/gawk/manual/gawk.html#Variable-Typing

"A string constant or the result of a string operation has the string
attribute."  (substr() is a string operation)

In the following matrix, you see that when an operand has string attribute
awk always performs a string comparison, regardless of the type of the
other operand.

-- 
D.



reply via email to

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