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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!jvnc.net!princeton!mccc!pjh
  3. From: pjh@mccc.edu (Pete Holsberg)
  4. Subject: Re: Associative arrays in [gn]awk --whynot! (long but interesting)
  5. Organization: The College On The Other Side Of Route One
  6. Date: Fri, 14 Aug 1992 17:39:52 GMT
  7. Message-ID: <1992Aug14.173952.12112@mccc.edu>
  8. References: <1992Aug13.063348.11691@news.acns.nwu.edu>
  9. Lines: 88
  10.  
  11. In article <1992Aug13.063348.11691@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
  12. =Well, after my previous posting on [gn]awk associative arrays I decided
  13. =to spend a little time investigating them. Here is what I came  up with. 
  14.  
  15. =======================
  16. =Here is a program that makes an associative array between the first and
  17. =fifth fields of a /etc/passwd-like file.
  18. =
  19. =awk '
  20. =BEGIN { FS=":"}
  21. =
  22. ={
  23. =#make asscociative array from first and fifth fields
  24. =fullname[$1]=$5
  25. =}
  26. =END {
  27. =   for ( username in fullname) 
  28. =           printf("%-10s %s \n", username,  fullname[username]);
  29. =}' short.passwd 
  30. =
  31. =awk:    results with just 15 numbers (from above)
  32. =1root      Operator        13 
  33. =11pib      Philip Burns        2
  34. =5usenet    Mr. Usenet          14
  35. =7accman    Account Manager      3 
  36. =14dave     Dave Wenger        15 
  37. =6server    Mr. Listserv        4 
  38. =13len      Leonard Evens    5 
  39. =12jln      John Norstad        6 
  40. =3games     Mr. Games        7 
  41. =4ftp       Mr. ftp        8 
  42. =10mccoy    James McCoy        9 
  43. =9matt      Matthew Larson    10 
  44. =8nims      Christopher Nims    11 
  45. =2news      Mr. News        12
  46. =15navarra  John Navarra     1
  47. =
  48. =nawk:
  49. =12jln      John Norstad        2 
  50. =7accman    Account Manager    3 
  51. =4ftp       Mr. ftp        4 
  52. =10mccoy    James McCoy        5 
  53. =14dave     Dave Wenger        6 
  54. =11pib      Philip Burns        7 
  55. =5usenet    Mr. Usenet        8 
  56. =2news      Mr. News        9 
  57. =6server    Mr. Listserv        10 
  58. =1root      Operator        11 
  59. =8nims      Christopher Nims    12 
  60. =3games     Mr. Games        13 
  61. =15navarra  John Navarra        14 
  62. =9matt      Matthew Larson    15 
  63. =13len      Leonard Evens    1 
  64. =
  65. =gawk:
  66. =10mccoy    James McCoy        1 
  67. =6server    Mr. Listserv        2 
  68. =5usenet    Mr. Usenet        3 
  69. =15navarra  John Navarra        4 
  70. =4ftp       Mr. ftp        5 
  71. =12jln      John Norstad        6 
  72. =9matt      Matthew Larson    7 
  73. =11pib      Philip Burns        8 
  74. =13len      Leonard Evens    9 
  75. =14dave     Dave Wenger        10 
  76. =3games     Mr. Games        11 
  77. =8nims      Christopher Nims    12 
  78. =2news      Mr. News        13 
  79. =7accman    Account Manager    14 
  80. =1root      Operator        15 
  81. =    
  82. =    This is truly weird.
  83.  
  84. Where did the values in the third field of the output come from? What's
  85. weird about the first two columns? It seems to me that they match
  86. insofar as the index and the value go.
  87.  
  88. =Basically, associative arrays in gn[awk] are 
  89. =unreliable (especially for a multi-field file).
  90.  
  91. Unreliable? In what way?
  92.  
  93. =Gawk seems to be the
  94. =best at printing out arrays in the order which they were loaded in but
  95. =it does not do it correctly in all cases. 
  96.  
  97. Order in "for (index in array)" is not specified by awk. If you need
  98. order, pipe the output to "sort".
  99.