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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!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: Associative arrays in [gn]awk --whynot! (long but interesting)
  5. Message-ID: <1992Aug13.063348.11691@news.acns.nwu.edu>
  6. Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
  7. Organization: Northwestern University, Evanston Illinois.
  8. Date: Thu, 13 Aug 1992 06:33:48 GMT
  9. Lines: 203
  10.  
  11. Well, after my previous posting on [gn]awk associative arrays I decided
  12. to spend a little time investigating them. Here is what I came  up with. 
  13. I ran the following little awk script on a few files:
  14. {
  15. array[NR] = $1
  16. }
  17. END {
  18.       for ( item in array)
  19.           printf("%s\n", array[item] )
  20. }
  21.  
  22.  
  23. I ran it on files with 10,11,12,15 and 21 records where each record was
  24. separated by a newline. Here are the results from the files with just
  25. numbers in them. Basically, you can replace the numbers by anything 
  26. because no sorting is done by any of the programs before the arrays
  27. are printed. We see some interesting behavior from both awk and nawk.
  28. Gawk works as expected -- printing the array out as it was read in.
  29. However, gawk did not work this way when more than one field was present
  30. in a file. (more on that below) 
  31.  
  32. awk   nawk    gawk
  33. =====================10 file===============
  34. 2    2    1
  35. 3    3    2
  36. 4    4    3
  37. 5    5    4
  38. 6    6    5
  39. 7    7    6
  40. 8    8    7
  41. 9    9    8
  42. 10    10    9
  43. 1    1    10
  44. =====================11 file===============
  45. 2    2    1
  46. 3    3    2
  47. 4    4    3
  48. 5    5    4
  49. 6    6    5
  50. 7    7    6
  51. 8    8    7
  52. 9    9    8
  53. 10    10    9
  54. 11    11    10
  55. 1    1    11
  56. =====================12 file===============
  57. 2    2    1
  58. 3    3    2
  59. 4    4    3
  60. 5    5    4
  61. 6    6    5
  62. 7    7    6
  63. 8    8    7
  64. 9    9    8
  65. 10    10    9
  66. 11    11    10
  67. 12    12    11
  68. 1    1    12
  69. =====================15 file===============
  70. 13    2    1
  71. 2    3    2
  72. 14    4    3
  73. 3    5    4
  74. 15    6    5
  75. 4    7    6
  76. 5    8    7
  77. 6    9    8
  78. 7    10    9
  79. 8    11    10
  80. 9    12    11
  81. 10    13    12
  82. 11    14    13
  83. 12    15    14
  84. 1    1    15
  85. =====================21 file===============
  86. 13    2    1
  87. 2    3    2
  88. 14    4    3
  89. 3    5    4
  90. 15    6    5
  91. 4    7    6
  92. 16    8    7
  93. 5    9    8
  94. 17    10    9
  95. 6    11    10
  96. 18    12    11
  97. 7    13    12
  98. 19    14    13
  99. 8    15    14
  100. 9    16    15
  101. 10    17    16
  102. 20    18    17
  103. 11    19    18
  104. 21    20    19
  105. 12    21    20
  106. 1    1    21
  107.  
  108. ======================
  109. Here is a program that makes an associative array between the first and
  110. fifth fields of a /etc/passwd-like file.
  111.  
  112. awk '
  113. BEGIN { FS=":"}
  114.  
  115. {
  116. #make asscociative array from first and fifth fields
  117. fullname[$1]=$5
  118. }
  119. END {
  120.    for ( username in fullname) 
  121.            printf("%-10s %s \n", username,  fullname[username]);
  122. }' short.passwd 
  123.  
  124.  
  125. Here is the passwd-like file:
  126. 1root:##root:0:1:Operator:/:/bin/csh
  127. 2news:##news:6:6:Mr. News:/usr/local/lib/news:/bin/sh
  128. 3games:##games:11:10:Mr. Games:/usr/games:/bin/false
  129. 4ftp:##ftp:12:10:Mr. ftp:/usr/spool/ftp:/bin/false
  130. 5usenet:##usenet:20:12:Mr. Usenet:/var/spool/news:/bin/true
  131. 6server:*:21:10:Mr. Listserv:/usr/server:/bin/tcsh
  132. 7accman:##accman:284:30:Account
  133. Manager:/usr/local/lib/cas:/usr/local/bin/cas
  134. 8nims:##nims:336:10:Christopher Nims:/home/u1/nims:/bin/tcsh
  135. 9matt:##matt:337:10:Matthew Larson:/home/u1/matt:/bin/tcsh
  136. 10mccoy:##mccoy:339:10:James McCoy:/home/u1/mccoy:/bin/tcsh
  137. 11pib:##pib:340:20:Philip Burns:/home/u1/pib:/bin/tcsh
  138. 12jln:##jln:341:20:John Norstad:/home/u1/jln:/bin/csh
  139. 13len:##len:343:20:Leonard Evens:/home/u1/len:/bin/csh
  140. 14dave:##dave:345:20:Dave Wenger:/home/u2/dave:/bin/tcsh
  141. 15navarra:##navarra:453:20:John Navarra:/home/u3/navarra:/bin/bash
  142.  
  143. Notice that I numbered the entries so you can see how out of order
  144. they are with awk, nawk, and gawk versions:
  145.  
  146. awk:                results with just 15 numbers (from above)
  147. 1root      Operator        13 
  148. 11pib      Philip Burns        2
  149. 5usenet    Mr. Usenet          14
  150. 7accman    Account Manager      3 
  151. 14dave     Dave Wenger        15 
  152. 6server    Mr. Listserv        4 
  153. 13len      Leonard Evens    5 
  154. 12jln      John Norstad        6 
  155. 3games     Mr. Games        7 
  156. 4ftp       Mr. ftp        8 
  157. 10mccoy    James McCoy        9 
  158. 9matt      Matthew Larson    10 
  159. 8nims      Christopher Nims    11 
  160. 2news      Mr. News        12
  161. 15navarra  John Navarra     1
  162.  
  163. nawk:
  164. 12jln      John Norstad        2 
  165. 7accman    Account Manager    3 
  166. 4ftp       Mr. ftp        4 
  167. 10mccoy    James McCoy        5 
  168. 14dave     Dave Wenger        6 
  169. 11pib      Philip Burns        7 
  170. 5usenet    Mr. Usenet        8 
  171. 2news      Mr. News        9 
  172. 6server    Mr. Listserv        10 
  173. 1root      Operator        11 
  174. 8nims      Christopher Nims    12 
  175. 3games     Mr. Games        13 
  176. 15navarra  John Navarra        14 
  177. 9matt      Matthew Larson    15 
  178. 13len      Leonard Evens    1 
  179.  
  180. gawk:
  181. 10mccoy    James McCoy        1 
  182. 6server    Mr. Listserv        2 
  183. 5usenet    Mr. Usenet        3 
  184. 15navarra  John Navarra        4 
  185. 4ftp       Mr. ftp        5 
  186. 12jln      John Norstad        6 
  187. 9matt      Matthew Larson    7 
  188. 11pib      Philip Burns        8 
  189. 13len      Leonard Evens    9 
  190. 14dave     Dave Wenger        10 
  191. 3games     Mr. Games        11 
  192. 8nims      Christopher Nims    12 
  193. 2news      Mr. News        13 
  194. 7accman    Account Manager    14 
  195. 1root      Operator        15 
  196.     
  197.     This is truly weird. I got the same results each time I ran
  198. the program on the file for each of awk, nawk, and gawk. Each is completely
  199. different from the other program and each is completely different from
  200. its own result of 15 numbers from the test above. 
  201.  
  202. I ran these test on a Sun running 4.1.2. If you want to try running the
  203. above programs on your machine and send me the results, I would be 
  204. glad to look at them. Basically, associative arrays in gn[awk] are 
  205. unreliable (especially for a multi-field file). Gawk seems to be the
  206. best at printing out arrays in the order which they were loaded in but
  207. it does not do it correctly in all cases. 
  208.  
  209. -tms
  210. -- 
  211. From the Lab of the MaD ScIenTiST:
  212.                     
  213. navarra@casbah.acns.nwu.edu        
  214.