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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cs.utexas.edu!wupost!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
  3. From: navarra@casbah.acns.nwu.edu (John Navarra)
  4. Subject: Re: Associative arrays in [gn]awk --whynot! (long but interesting)
  5. Message-ID: <1992Aug15.021954.27159@news.acns.nwu.edu>
  6. Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
  7. Organization: Northwestern University, Evanston Illinois.
  8. References: <1992Aug13.063348.11691@news.acns.nwu.edu> <1992Aug14.173952.12112@mccc.edu>
  9. Date: Sat, 15 Aug 1992 02:19:54 GMT
  10. Lines: 100
  11.  
  12. In article <1992Aug14.173952.12112@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes
  13. >In article <1992Aug13.063348.11691@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
  14. >=======================
  15. >=Here is a program that makes an associative array between the first and
  16. >=fifth fields of a /etc/passwd-like file.
  17. >
  18. >Where did the values in the third field of the output come from? What's
  19. >weird about the first two columns? It seems to me that they match
  20. >insofar as the index and the value go.
  21.  
  22.     The third field was a replication of the results from printing out
  23. the values stored in the array for a 15 line file (numbered 1-15). I just
  24. wanted to point out the a one column (or one-field per line file) is output
  25. differently than a multi-column (or multi-field per line) file with each
  26. of the programs awk, nawk, and gawk.
  27.  
  28. >=Basically, associative arrays in gn[awk] are 
  29. >=unreliable (especially for a multi-field file).
  30. >
  31. >Unreliable? In what way?
  32.  
  33.     With respect to order being read in. Obviously, many of you know
  34. that this is a well-known bug. Some do not. However, gawk, which you may
  35. expect to print out the values correctly (i.e in numerical order) only 
  36. prints those values correctly if the file is in numerical order in the first 
  37. place. This is not always going to be the situation! 
  38.  
  39. >
  40. >=Gawk seems to be the
  41. >=best at printing out arrays in the order which they were loaded in but
  42. >=it does not do it correctly in all cases. 
  43. >
  44. >Order in "for (index in array)" is not specified by awk. If you need
  45. >order, pipe the output to "sort".
  46.  
  47.     Yeah, but what if sort doesn't cut it? For instance, say I had the
  48. file:
  49.  
  50. 2
  51. 1
  52. 4
  53. 3
  54. 6
  55. 5
  56. 8
  57. 7
  58. 10
  59. 9
  60. 12
  61. 11
  62. 14
  63. 13
  64. 15
  65.  
  66. and I ran my program on it:
  67. $ gawk -f showarray 15nums.mixed
  68. 2
  69. 1
  70. 4
  71. 3
  72. 6
  73. 5
  74. 8
  75. 7
  76. 10
  77. 9
  78. 12
  79. 11
  80. 14
  81. 13
  82. 15
  83.  
  84. This is not the correct order.
  85. $ gawk -f showarray 15nums.mixed | sort -n
  86. 1
  87. 2
  88. 3
  89. 4
  90. 5
  91. 6
  92. 7
  93. 8
  94. 9
  95. 10
  96. 11
  97. 12
  98. 13
  99. 14
  100. 15
  101.  
  102.     and this is not the correct order either. I *want* the output to
  103. be printed in the *same* order it was read in!!! Now tell me how to do
  104. that with sort if the numbers aren't in some order sort understands?
  105. Now see where the problem is?  
  106.  
  107. -tms
  108. -- 
  109. From the Lab of the MaD ScIenTiST:
  110.                     
  111. navarra@casbah.acns.nwu.edu        
  112.