[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: |
Sun, 8 May 2022 23:01:26 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hi,
On Fri, May 06, 2022 at 05:28:39PM -0500, Peng Yu wrote:
> I want to make sure whether the following code is guaranteed to
> compare $1 and $2 numerically as long as the input of the 1st and 2nd
> fields are legitimate numbers or empty strings (which are considered
> as 0).
>
> awk -e '{ print ($1 < $2) }' < input
>
> Or there is any corner case when even the 1st and 2nd fields are
> legitimate numbers or empty strings, they may be compared as strings.
>
> Note although an empty string compared with an empty string is a
> string comparison, the result is the same as 0 and 0 comparison, so it
> is still effectively considered as a numerical comparison for the
> current discussion.
This discussion has gone off the rails. If you've got user input
of an empty string, then gawk will consider it to be a string, not
a number, and the comparision will be a string comparison. If you
want to ensure a numeric comparison, you must coerce both arguments
to be numbers. This is typically done by adding zero.
It's explained by this table in the docs:
When two operands are compared, either string comparison or numeric
comparison may be used. This depends upon the attributes of the
operands, according to the following symmetric matrix:
+----------------------------------------------
| STRING NUMERIC STRNUM
--------+----------------------------------------------
|
STRING | string string string
|
NUMERIC | string numeric numeric
|
STRNUM | string numeric numeric
--------+----------------------------------------------
And by this bit of sample code showing that an empty string is considered
to be a string:
bash-4.2$ echo 1::b | gawk -F: '{print typeof($1), typeof($2), typeof($3)}'
strnum string string
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?
Regards,
Andy
- ensure numeric comparison, Peng Yu, 2022/05/06
- Re: ensure numeric comparison, david kerns, 2022/05/06
- Re: ensure numeric comparison, Peng Yu, 2022/05/07
- Re: ensure numeric comparison, david kerns, 2022/05/07
- Re: ensure numeric comparison, Peng Yu, 2022/05/08
- Re: ensure numeric comparison, Neil R. Ormos, 2022/05/08
- Re: ensure numeric comparison, Peng Yu, 2022/05/08
- Re: ensure numeric comparison, Neil R. Ormos, 2022/05/08
Re: ensure numeric comparison,
Andrew J. Schorr <=