help-gawk
[Top][All Lists]
Advanced

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

Re: ensure numeric comparison


From: david kerns
Subject: Re: ensure numeric comparison
Date: Fri, 6 May 2022 15:43:41 -0700

pretty sure the only way to guarantee numeric comparison is to add 0

awk -e '{ print ($1 +0 < $2 + 0) }' < input

(not sure if it's required on both sides, but it can't hurt)

On Fri, May 6, 2022 at 3:28 PM Peng Yu <pengyu.ut@gmail.com> wrote:

> Hi,
>
> 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.
>
> --
> Regards,
> Peng
>
>


reply via email to

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