home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!cs.utexas.edu!wupost!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
- From: navarra@casbah.acns.nwu.edu (John Navarra)
- Subject: Re: Associative arrays in [gn]awk --whynot! (long but interesting)
- Message-ID: <1992Aug15.021954.27159@news.acns.nwu.edu>
- Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
- Organization: Northwestern University, Evanston Illinois.
- References: <1992Aug13.063348.11691@news.acns.nwu.edu> <1992Aug14.173952.12112@mccc.edu>
- Date: Sat, 15 Aug 1992 02:19:54 GMT
- Lines: 100
-
- In article <1992Aug14.173952.12112@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes
- >In article <1992Aug13.063348.11691@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
- >=======================
- >=Here is a program that makes an associative array between the first and
- >=fifth fields of a /etc/passwd-like file.
- >
- >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.
-
- The third field was a replication of the results from printing out
- the values stored in the array for a 15 line file (numbered 1-15). I just
- wanted to point out the a one column (or one-field per line file) is output
- differently than a multi-column (or multi-field per line) file with each
- of the programs awk, nawk, and gawk.
-
- >=Basically, associative arrays in gn[awk] are
- >=unreliable (especially for a multi-field file).
- >
- >Unreliable? In what way?
-
- With respect to order being read in. Obviously, many of you know
- that this is a well-known bug. Some do not. However, gawk, which you may
- expect to print out the values correctly (i.e in numerical order) only
- prints those values correctly if the file is in numerical order in the first
- place. This is not always going to be the situation!
-
- >
- >=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".
-
- Yeah, but what if sort doesn't cut it? For instance, say I had the
- file:
-
- 2
- 1
- 4
- 3
- 6
- 5
- 8
- 7
- 10
- 9
- 12
- 11
- 14
- 13
- 15
-
- and I ran my program on it:
- $ gawk -f showarray 15nums.mixed
- 2
- 1
- 4
- 3
- 6
- 5
- 8
- 7
- 10
- 9
- 12
- 11
- 14
- 13
- 15
-
- This is not the correct order.
- $ gawk -f showarray 15nums.mixed | sort -n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
-
- and this is not the correct order either. I *want* the output to
- be printed in the *same* order it was read in!!! Now tell me how to do
- that with sort if the numbers aren't in some order sort understands?
- Now see where the problem is?
-
- -tms
- --
- From the Lab of the MaD ScIenTiST:
-
- navarra@casbah.acns.nwu.edu
-