2

I know, the subscript of awk array must be a string.

[root@localhost]# awk 'END {array[A0]="empty"; print array[""]}'
empty

So in above command line, because A0 is not quoted as "A0" , it stands for a variable. Because the variable A0 hasn't been set value before, the value is "". So the print array[""] outputs empty.

But in the following command:

[root@localhost]#  awk 'END {array[0]="empty"; print array[""], array["0"]}'
 empty

The value of array[""] is NULL while array["0"] is "empty". Per my understanding, that's because the variable can't begin with digit, array[0] is converted to array["0"] by default. Is it right? What is the rule of converting subscript of awk array?

0

1 Answer 1

4

Array subscripts in awk are strings, so when you use an expression as array subscript, it is converted to a string (if it isn't one already). 0 is a number, not a variable, so the following applies (from POSIX):

A numeric value that is exactly equal to the value of an integer (see Concepts Derived from the ISO C Standard) shall be converted to a string by the equivalent of a call to the sprintf function (see String Functions) with the string "%d" as the fmt argument and the numeric value being converted as the first and only expr argument. Any other numeric value shall be converted to a string by the equivalent of a call to the sprintf function with the value of the variable CONVFMT as the fmt argument and the numeric value being converted as the first and only expr argument. The result of the conversion is unspecified if the value of CONVFMT is not a floating-point format specification.

0 is an integer, so it gives "0" when converted to string, not "". This is because in C code, after sprintf(buf, "%d", 0), buf would contain the string "0".

As for variable names: in the awk grammar, variables are described by the token NAME. The lexicographical convention for it are as follows:

9) A sequence of underscores, digits, and alphabetics from the portable character set (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 6.1, Portable Character Set), beginning with an underscore or alphabetic, shall be considered a word.

12) The token NAME shall consist of a word that is not a keyword or a name of a built-in function and is not followed immediately (without any delimiters) by the '(' character.

Tokens that follow this description are variables, initially empty, and when an empty variable is converted to string, it yields the empty string.

To wit:

  • 0 is a number
  • a is a variable name
  • _ is a variable name
  • a0 is a variable name
  • _0 is a variable name
  • 0a is parsed as 0 a (the concatenation of 0 and the variable a)
6
  • 1
    0a is not a syntax error, it evaluates to just 0a when used as a subscript. Try awk '{a="c";b[0a]}END{for (i in b)print i}' file and you will see it prints 0c
    – user4453924
    Apr 13, 2015 at 10:02
  • Oh, interesting, and rather unexpected. Do you know whether that is required by POSIX or gawk/mawk-specific?
    – Wintermute
    Apr 13, 2015 at 11:17
  • Funky. As far as I can see, it is required by POSIX, and the lexical convention "An integer constant cannot begin with 0x or include the hexadecimal digits 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', or 'F'." leads me to believe that this is intentional. This means that given the code { xf="foo"; print 0xf }, gawk's behavior of printing 15 is non-standard while mawk is compliant by printing 0foo.
    – Wintermute
    Apr 13, 2015 at 11:31
  • Yes, allowing the use of hex (and octal) constants (e.g. 0xf) in your script is a gawk extension, see gnu.org/software/gawk/manual/gawk.html#Nondecimal_002dnumbers, and can be disabled with the --posix flag in the unlikely event that the resultant behavior is undesirable. Note that means that a[021]; for (i in a) print i in gawk will print 17.
    – Ed Morton
    Apr 13, 2015 at 12:27
  • @EdMorton, does that mean --non-decimal-data is defined by default? Or does option that mean something different than what we are talking about?
    – jas
    Apr 13, 2015 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.