[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to convert a string to strnum?
From: |
Neil R. Ormos |
Subject: |
Re: How to convert a string to strnum? |
Date: |
Sun, 5 Sep 2021 00:40:38 -0500 (CDT) |
Peng Yu wrote:
> Neil R. Ormos 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. [...]
> It sounds like a function named like
> str2strnum(), other a hack using match(), is
> more intuitive.
Attention is yet again drawn to the manual.
Section 9.2 (currently), entitled "User-Defined
Functions," explains how one can define a function
and assign a nearly arbitrary name to it.
It is observed that the name "str2strnum" is
available for use as a user-defined function in
gawk version 5.1.0.