I have the gawk version of awk. In this part of gawk manual, it is stated that awk variables have "attributes", which are used to determine how to treat them in various operations.
For example, a string that is of the form " +3.14"
which is obtained by parsing the input has the STRNUM
attribute, which makes it behave as a number in a comparison with a number, whereas the same string defined in an awk program does not have this attribute.
OTOH, a string like "3.14"
apparently has STRNUM
attribute, even if it was defined in the program because the code x = "3.14" { print x == 3.14 }
prints 1. Whereas if we define it as "+3.13"
or " 3.14"
, it does not have STRNUM
attribute since the x = "+3.14" { print x == 3.14 }
or x = " 3.14" { print x == 3.14 }
prints 0.
I think that such succinctness in variable typing may cause subtle bugs. Hence, in order to aid in debugging such situations, is there a way to learn what type of "attributes" a variable has? I.e, can we learn what is the type of a variable?
Best Answer
Awk has 4 types: "number", "string", "numeric string" and "undefined". Here is a function to detect that:
For the third argument of
split
, you need something that is not a space, and not part ofobj
or else it will be treated as a delimiter. I chose\1
based on Stéphane suggestion. The function does internalCONVFMT
toggling, so it should return the correct result regardless ofCONVFMT
value at the time of the function call:Result:
Full test suite:
Result:
The notable weakness is: if you provide "numeric string" of any of the following, the function will incorrectly return "number":
inf
-inf
For integers, this is explained:
However
inf
and-inf
behave this way as well; that is to say that none of the above can be influenced by theCONVFMT
variable:Result:
In practice this doesn’t really matter, see the Duck test.