home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!jvnc.net!princeton!mccc!pjh
- From: pjh@mccc.edu (Pete Holsberg)
- Subject: Re: Associative arrays in [gn]awk --whynot! (long but interesting)
- Organization: The College On The Other Side Of Route One
- Date: Fri, 14 Aug 1992 17:39:52 GMT
- Message-ID: <1992Aug14.173952.12112@mccc.edu>
- References: <1992Aug13.063348.11691@news.acns.nwu.edu>
- Lines: 88
-
- In article <1992Aug13.063348.11691@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
- =Well, after my previous posting on [gn]awk associative arrays I decided
- =to spend a little time investigating them. Here is what I came up with.
-
- =======================
- =Here is a program that makes an associative array between the first and
- =fifth fields of a /etc/passwd-like file.
- =
- =awk '
- =BEGIN { FS=":"}
- =
- ={
- =#make asscociative array from first and fifth fields
- =fullname[$1]=$5
- =}
- =END {
- = for ( username in fullname)
- = printf("%-10s %s \n", username, fullname[username]);
- =}' short.passwd
- =
- =awk: results with just 15 numbers (from above)
- =1root Operator 13
- =11pib Philip Burns 2
- =5usenet Mr. Usenet 14
- =7accman Account Manager 3
- =14dave Dave Wenger 15
- =6server Mr. Listserv 4
- =13len Leonard Evens 5
- =12jln John Norstad 6
- =3games Mr. Games 7
- =4ftp Mr. ftp 8
- =10mccoy James McCoy 9
- =9matt Matthew Larson 10
- =8nims Christopher Nims 11
- =2news Mr. News 12
- =15navarra John Navarra 1
- =
- =nawk:
- =12jln John Norstad 2
- =7accman Account Manager 3
- =4ftp Mr. ftp 4
- =10mccoy James McCoy 5
- =14dave Dave Wenger 6
- =11pib Philip Burns 7
- =5usenet Mr. Usenet 8
- =2news Mr. News 9
- =6server Mr. Listserv 10
- =1root Operator 11
- =8nims Christopher Nims 12
- =3games Mr. Games 13
- =15navarra John Navarra 14
- =9matt Matthew Larson 15
- =13len Leonard Evens 1
- =
- =gawk:
- =10mccoy James McCoy 1
- =6server Mr. Listserv 2
- =5usenet Mr. Usenet 3
- =15navarra John Navarra 4
- =4ftp Mr. ftp 5
- =12jln John Norstad 6
- =9matt Matthew Larson 7
- =11pib Philip Burns 8
- =13len Leonard Evens 9
- =14dave Dave Wenger 10
- =3games Mr. Games 11
- =8nims Christopher Nims 12
- =2news Mr. News 13
- =7accman Account Manager 14
- =1root Operator 15
- =
- = This is truly weird.
-
- Where did the values in the third field of the output come from? What's
- weird about the first two columns? It seems to me that they match
- insofar as the index and the value go.
-
- =Basically, associative arrays in gn[awk] are
- =unreliable (especially for a multi-field file).
-
- Unreliable? In what way?
-
- =Gawk seems to be the
- =best at printing out arrays in the order which they were loaded in but
- =it does not do it correctly in all cases.
-
- Order in "for (index in array)" is not specified by awk. If you need
- order, pipe the output to "sort".
-