help-gawk
[Top][All Lists]
Advanced

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

Re: ensure numeric comparison


From: Andrew J. Schorr
Subject: Re: ensure numeric comparison
Date: Tue, 10 May 2022 16:24:08 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, May 09, 2022 at 11:36:14AM -0500, Peng Yu wrote:
> It is not always the case. Empty strings may be turned into
> "unassigned". See below for a test case. That's why I want to ask to
> make sure if there are corner cases. Obviously, it is hard not to miss
> corner cases, as you missed this one :)
> 
> $ awk -e '{ print typeof($1) }' <<< ''
> unassigned

No. There's a difference between an uninitialized variable and
an empty string. An empty string is "". It's an empty string.
But a variable that has never been assigned is a null value,
which has hybrid qualities of a string and number. It's considered
either string "" or numeric 0, depending on the context.

> > The beauty of gawk is that it's very easy to test and get your answer:
> >
> > bash-4.2$ echo :-1 | gawk -F: '{print ($1 < $2)}'
> > 1
> > bash-4.2$ echo :-1 | gawk -F: '{print ($1+0 < $2+0)}'
> > 0
> >
> > Is everything now crystal clear?
> 
> OK. I got it.
> 
> When "unassigned" is included, what is the comparison table look like?

Why are you comparing unassigned values? What are you trying to
accomplish? If you compare an unassigned value to a string, it will
compare as an empty string. But if you compare it to a number, it will
compare numerically as zero.

Regards,
Andy



reply via email to

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