[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Gawk Reference Card distributed with gawk 3.0.98
From: |
erik |
Subject: |
Gawk Reference Card distributed with gawk 3.0.98 |
Date: |
Thu, 24 May 2001 16:43:08 +1000 |
G'day,
The card is splendid and helpful, but the Arrays stuff on page 11 may
mislead, I think. i.e.
>>>
If the array has multiple subscripts, use (i,j) in array.
Use the in construct in a for loop to iterate over all the elements of an
array
<<<
Iterating over a pseudo-multidimensional array is successful the old way:
#!/bin/sh
gawk '
BEGIN { A[1,1,1] = "A"
A[1,1,4] = "B"
A[1,3,1] = "C"
A[2,1,1] = "D"
for (i in A)
{ split(i,B,SUBSEP) # A la Addison-Wesley book, by A&W&K.
printf("%s:%s:%s:-> %s\n",B[1],B[2],B[3],A[i])
}
# for ((i,j,k) in A)
# { printf("%s:%s:%s:-> %s\n",i,j,k,A[i,j,k])
# }
}'
giving (with gawk 3.0.98):
1:3:1:-> C
1:1:1:-> A
1:1:4:-> B
2:1:1:-> D
but once uncommented, in naive reliance on page 11, we have:
gawk: cmd. line:12: for ((i,j,k) in A)
gawk: cmd. line:12: ^ parse error
If I am missing something, then we might still benefit from the
addition of a clue to that section. Neh?
Otherwise, the second quoted Reference card sentence, above, could profitably
be replaced with something along the lines of:
>>>
To iterate over an array: for (n in x)
If "multi-dimensional", may need: { split(n,A,SUBSEP)
Now A[1] = i, A[2] = j, A[3] = k, if x is the array above.
<<<
Waddaya reckon?
Regards,
Erik
================================================================================
_,-_|\ Erik Christiansen
/ \ RESEARCH & DEVELOPMENT DIVISION
\_,-.__/ Emedded Systems Development Department
v NEC Business Solutions Pty. Ltd.
Private Bag 5555, Phone: +61 3 9264 3416
Mulgrave 3170, Fax: +61 3 9264 3841
Victoria, Australia.
email: address@hidden
NEC home page: http://www.nec.com.au/
===============================================================================
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Gawk Reference Card distributed with gawk 3.0.98,
erik <=