help-gawk
[Top][All Lists]
Advanced

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

Re: How to convert a string to strnum?


From: Peng Yu
Subject: Re: How to convert a string to strnum?
Date: Sat, 4 Sep 2021 23:02:53 -0500

It sounds like a function named like str2strnum(), other a hack using
match(), is more intuitive.

On 9/4/21, Neil R. Ormos <ormos-gnulists17@ormos.org> wrote:
> Peng Yu wrote:
>
>> https://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html
>
>> $ awk -e 'BEGIN { x="1e6"; print typeof(x), typeof(strtonum(x)) }'
>> string number
>
>> I can only convert a string to a number. But I
>> don't find a way to convert a string to a
>> strnum. Is there a way to do so?
>
> shell> gawk 'BEGIN{ a="1e6"; z=match(a, /.*/, r); print "a:", typeof(a), a;
> print "r:", typeof(r[0]), r[0], r[0]*1;}'
>
> a: string 1e6
> r: strnum 1e6 1000000
>
> Note that the answer the poster sought was in the
> very section of the manual the poster himself/herself
> cited:
>
> | [...] Fields, getline input, FILENAME, ARGV
> | elements, ENVIRON elements, and the elements of
> | an array created by match(), split(), and
> | patsplit() that are numeric strings have the
> | strnum attribute. [...]
>
>


-- 
Regards,
Peng



reply via email to

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