[H-GEN] one for an awk guru
Mark J. Suter
mark at zwitterion.humbug.org.au
Tue Feb 3 21:51:22 EST 1998
-----BEGIN PGP SIGNED MESSAGE-----
On Wed, 4 Feb 1998, Michael Dooley wrote:
> Here's something strange I've seen with awk and nawk a
> couple of time now. In this case I'm finding lines from headers of
> files where $1 is SSBOND and creating an array of $4's and $6's. Here's
> some sample lines:
[ snip ]
> If I use the alternative 'for' definition : for (m=1;m<=x;m++), I get
> the output I want. However x will vary from file to file so I'd rather
> use the array specification of for.
>
> Is this a case of the script giving me what I ask for, not what I want
> (??!!) any ideas?
Benjamin Carlyle's explanation about hash-tables was correct, from a
certain point of view. The hash-tables are an implementation issue.
The true source of the lack of ordering lies in the nature of the
arrays, not how they are stored.
Arrays in awk are different from those in most other programming
languages, such as C: they are associative. With an associative array,
order is irrelevant! Thus, hash-tables which don't preserve order
are a suitable means of storing a data structure for which order
is unimportant.
The `for e in array' construct is used to iterate over the entire
array, without regard to order, as is appropriate for an associative
array.
For more detail, see the documentation for a explanation of gawk's
associative arrays at http://www.tec.ualberta.ca/Documentation
/Info/by-chapter/gawk-3.0.3/gawk_12.html
Yours sincerely,
Mark Suter.
- -- Mark John Suter | I know that you believe you understand
suter at humbug.org.au | what you think I said, but I am not sure
PGP encryption is OK | you realise that what you heard is not
Ph: +61 7 3891 2085 | what I meant. anonymous
-----BEGIN PGP SIGNATURE-----
Version: 2.6.3i
Charset: noconv
Comment: Public key available from http://student.uq.edu.au/~cs331007
iQCVAgUBNNfXpnBbxiOCJ17ZAQGXTQQAx7x0gN2Vg4FRPrIMTTZj5S6YcP22WMC6
yi3C5JBb5atMBeqrGArhmAEp3C2mvr6YqOHZgc3he2ExyZ5AqwWROIqvzrOwVmXF
tHHDr5yCTp8Y6u2RHD6yOArlaT2HuW/+8Vg6JLr0q8dhzVz4OHFtzCDtjvX9rHe9
/Fv7R12xTKU=
=DxEX
-----END PGP SIGNATURE-----
More information about the General
mailing list