bug-gawk
[Top][All Lists]
Advanced

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

Re: How to check if a field is a number (integer or float)?


From: Andrew J. Schorr
Subject: Re: How to check if a field is a number (integer or float)?
Date: Sun, 20 Jun 2021 08:28:25 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Sat, Jun 19, 2021 at 10:35:48AM -0500, Peng Yu wrote:
> The best solution that I figured out is just `typeof(x) == "strnum"`||
> typeof(x) == "number"` instead of using regex.

That works as long as the value was assigned properly. If it was assigned as a
pure string, then it won't be recognized as containing a numeric value. To be
clear:

bash-4.2$ gawk -v "x=foo" 'BEGIN {print typeof(x)}'
string

bash-4.2$ gawk -v "x=4" 'BEGIN {print typeof(x)}'
strnum

bash-4.2$ gawk 'BEGIN {x = 4; print typeof(x)}'
number

bash-4.2$ gawk 'BEGIN {x = "4"; print typeof(x)}'
string

Regards,
Andy



reply via email to

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