home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10266 < prev    next >
Encoding:
Text File  |  1992-08-21  |  2.8 KB  |  70 lines

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