Timeline for Why does gawk treat `0123` as a decimal number when coming from the input data?
Current License: CC BY-SA 4.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 11, 2020 at 14:16 | history | edited | CommunityBot | Commonmark migration | |
| Feb 26, 2019 at 14:45 | comment | added | user313992 | @user938271 an explicit or implicit split() in awk will create "dual-nature" values which are both strings and numbers. This does not happen with other functions: awk 'BEGIN{s="0 1 2"; split(s, a); z = substr(s, 1, 1); print a[1], z, (a[1] == z), a[1] ? "yes" : "no", z ? "yes" : "no"}'. Same thing with $1 as with a[1]. As I already mentioned in a couple of comments / answers, this is not properly specified in the standard, but only vaguely alluded to. It probably deserves its own Q&A. | |
| Feb 26, 2019 at 14:44 | comment | added | Stéphane Chazelas | Note that if POSIXLY_CORRECT is set gawk treats 0x10 as a 16 number (as required (by mistake) by some older version of the POSIX spec, but now only allowed) | |
| Feb 26, 2019 at 14:32 | history | edited | Stephen Kitt | CC BY-SA 4.0 | Add the rules for what constitudes a number. |
| Feb 26, 2019 at 14:29 | comment | added | Stephen Kitt | See the description in the POSIX awk specification: a number is a possibly empty sequence of spaces, followed by “+” or “-”, followed by digits forming a floating-point decimal number. “x” isn’t allowed in a number, so it’s a string. | |
| Feb 26, 2019 at 14:13 | comment | added | user938271 | Thank you for the answer. So, the only way to bypass the fact that strtonum() looks for numbers first is to use a dummy string concatenation and force the numeric string to become a literal string: $ awk '{ print strtonum($1 "") }' <<<'0123'. Could you clarify which rule in the link of the user manual explains why 0x123 doesn't look like a number? Because it looks like a number to me; at least that's how I would write 291 in hexadecimal in an awk progam text. Is it because of the alphabetical character x? | |
| Feb 26, 2019 at 14:05 | vote | accept | user938271 | ||
| Feb 26, 2019 at 14:00 | history | answered | Stephen Kitt | CC BY-SA 4.0 |