[Top][All Lists]

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

Re: ensure numeric comparison

From: Peng Yu
Subject: Re: ensure numeric comparison
Date: Thu, 26 May 2022 11:58:54 -0500

On 5/10/22, Andrew J. Schorr <> wrote:
> 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

When I say "empty string", I am referring to '' in bash. The input is
an empty string in a line. But awk, once reading the input in,
considers it as unassigned. So you did miss this case in the
discussion. I just wanted to point it out.

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

The point is not users want to compare unassigned values. The point is
that  due to the input that may contain corner cases, unassigned
values can be passed to comparison operators. To make the manual
complete, the unassigned values should be added to the table.


reply via email to

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