- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2014-10-11 21:37 编辑
回复 1# aingwen
a[key] = value;
finally, key will be a string in awk
$ echo a b c | awk '{a[$1,$2]=$3;for(n in a)print n"="a[n]}' | hexdump -C
00000000 61 1c 62 3d 63 0a |a.b=c.|
Note: ASCII 0x1C is non-printing control charact
$ echo a b c | awk '{a[$1","$2]=$3;for(n in a)print n"="a[n]}' | hexdump -C
00000000 61 2c 62 3d 63 0a |a,b=c.|
===================================
$ echo a b c | awk 'function p(s){print s" ---";for(n in a)print n"="a[n]}BEGIN{a[01]="01N";a[02]="02N";a["01"]="01S";a["02"]="02S";p("at BEGIN")}{for(n=1;n<=NF;n++)a[n]=$n;p("at Line"NR)}END{a["1"]="1S";a["2"]="2S";p("at END")}'
at BEGIN ---
01=01S
02=02S
1=01N
2=02N
at Line1 ---
01=01S
02=02S
1=a
2=b
3=c
at END ---
01=01S
02=02S
1=1S
2=2S
3=c
=====================================
http://www.gnu.org/software/gawk/manual/gawk.html#Multidimensional
8.5 Multidimensional Arrays
• Multiscanning: Scanning multidimensional arrays.
A multidimensional array is an array in which an element is identified by a sequence of indices instead of a single index. For example, a two-dimensional array requires two indices. The usual way (in most languages, including awk) to refer to an element of a two-dimensional array named grid is with grid[x,y].
Multidimensional arrays are supported in awk through concatenation of indices into one string. awk converts the indices into strings (see Conversion) and concatenates them together, with a separator between them. This creates a single string that describes the values of the separate indices. The combined string is used as a single index into an ordinary, one-dimensional array. The separator used is the value of the built-in variable SUBSEP.
For example, suppose we evaluate the expression ‘foo[5,12] = "value"’ when the value of SUBSEP is "@". The numbers 5 and 12 are converted to strings and concatenated with an ‘@’ between them, yielding "5@12"; thus, the array element foo["5@12"] is set to "value".
Once the element’s value is stored, awk has no record of whether it was stored with a single index or a sequence of indices. The two expressions ‘foo[5,12]’ and ‘foo[5 SUBSEP 12]’ are always equivalent.
The default value of SUBSEP is the string "\034", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data. The usefulness of choosing an unlikely character comes from the fact that index values that contain a string matching SUBSEP can lead to combined strings that are ambiguous. Suppose that SUBSEP is "@"; then ‘foo["a@b", "c"]’ and ‘foo["a", "b@c"]’ are indistinguishable because both are actually stored as ‘foo["a@b@c"]’.
|
|