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 22:18:15 -0700

see 6.3.2.1 String Type versus Numeric Type
in https://www.gnu.org/software/gawk/manual/gawk.html

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

> Is there a test case for which `awk -e '{ print ($1 < $2) }' < input`
> can not compare $1 and $2 numerically given the input columes 1 and 2
> are numbers or empty strings?
>
> On 5/6/22, david kerns <david.t.kerns@gmail.com> wrote:
> > 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
> >>
> >>
> >
>
>
> --
> Regards,
> Peng
>


reply via email to

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