home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bcars267!bcarh803!christo
- From: christo@bcarh803.BNR.CA (Mark Christopher)
- Subject: Re: Associative arrays in [gn]awk --whynot! (long but interesting)
- Message-ID: <1992Aug21.152622.4819@bnr.ca>
- Sender: news@bnr.ca (usenet)
- Nntp-Posting-Host: bcarh803
- Reply-To: christo@bnr.ca
- Organization: Bell-Northern Research Ltd.
- References: <1992Aug13.063348.11691@news.acns.nwu.edu> <1992Aug14.173952.12112@mccc.edu> <1992Aug15.021954.27159@news.acns.nwu.edu>
- Date: Fri, 21 Aug 1992 15:26:22 GMT
- Lines: 56
-
- In article <1992Aug15.021954.27159@news.acns.nwu.edu>,
- navarra@casbah.acns.nwu.edu (John Navarra) writes:
- |>In article <1992Aug14.173952.12112@mccc.edu> pjh@mccc.edu (Pete
- Holsberg) writes
- |>>In article <1992Aug13.063348.11691@news.acns.nwu.edu>
- navarra@casbah.acns.nwu.edu (John Navarra) writes:
- |>>=======================
- ...
- |>>=Basically, associative arrays in gn[awk] are
- |>>=unreliable (especially for a multi-field file).
- |>>
- |>>Unreliable? In what way?
- |>
- |> With respect to order being read in. Obviously, many of you know
- |>that this is a well-known bug. Some do not. However, gawk, which you may
- |>expect to print out the values correctly (i.e in numerical order) only
- |>prints those values correctly if the file is in numerical order in the first
- |>place. This is not always going to be the situation!
-
- I think you are assuming too much. As far as I know, associative arrays
- have _never_ been guaranteed to be ordered. Let me dig out my Awk book by
- A, W, and K, and see what it says (dig dig dig dig)...
-
- "The AWK Programming Language", Alfred V Aho, Brian W Kernighan,
- Peter J Weinberger, 1988. ISBN 0-201-07981-X
-
- p.50: "The characteristic that sets awk arrays apart from those in
- most other languages is that subscripts are strings."
-
- p.51: " for (variable in array)
- statement
- This loop executes _statement_ with _variable_ set in turn to
- each different subscript in the array. The order in which the
- subscripts are considered in implementation dependent. Results
- are unpredictable if new elements are added to the array by
- _statement_."
-
- This last quote makes it pretty clear that what you describe is not a
- bug, it's a feature.
-
-
- ...
- |> and this is not the correct order either. I *want* the output to
- |>be printed in the *same* order it was read in!!! Now tell me how to do
- |>that with sort if the numbers aren't in some order sort understands?
- |>Now see where the problem is?
-
- Not a problem. Let me dig out an applicable quote here:
-
- Any problem in computer science can be solved with one more level of
- indirection. -- Morven's Metatheorum
-
-
- Have fun...
- --
- Mark Christopher Bell-Northern Research christo@bnr.ca
-