Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!sdd.hp.com!caen!dali.cs.montana.edu!milton!uw-beaver!ssc-vax!bcsaic!vince From: vince@bcsaic.UUCP (Vince Skahan) Newsgroups: comp.lang.perl Subject: printing assoc array in non-alphabetical order Message-ID: <46485@bcsaic.UUCP> Date: 3 May 91 21:21:01 GMT Organization: Boeing Aerospace and Electronics - Seattle Lines: 95 [...sigh...hope the previous Cancels on this took effect and you only got one...] I have an associative array sorting question (I think)... but it might be more related to keys. The question is "how can I get an associative array to print in a specified (by me) order that is NOT either alphabetic or reversed-alphabetic ?" The following fragment prints: net.apr net.feb net.jan net.mar when I want net.jan net.feb net.mar net.apr how can I get what I'm looking for in the following example ??? --------------------- example ------------------- %files=("net.jan",1,"net.feb",2,"net.mar",3,"net.apr",4); foreach $item (sort keys %files) { print "$item\n"; } ------------------------------------------------- let me give some more details if it helps... 1. I have a number of files, each with individual lines that begin with the month-of-the-year (or do so after I get through with splitting the 1st item per line), and the data looks like this: #monthname hostname bytes-in bytes-out errors char-per-sec Jan myhost 9000 3333 2 221 (etc...for many lines) 2. there are individual file names "net.monthname" for each month of the year that have to be reported on (but not necessarily processed) in "month-of-year order" 3. I am taking a given month's data, and summarizing it based on an associative array by processing it something like the follows: if $bytes{$monthname} isn't defined, init all the individual $somethings{$monthname} to 0 fill in the various $somethings{$monthname} with something like: $bytes_in{$monthname} += $2; calculate all the $monthname specific totals, rates, etc. that I want to generate. what I want to do is: 1. open the files if they exist (no sweat, I made a list of files "net.*" and didn't die on failure to open) 2. do "good" stuff with them (I can already do this part for a given filename and generate the assoc. array) 3. print the per-month totals as follows: filename category1 category2... net.jan $total1{$jan} $total2{$jan} net.feb $total1{$feb} $total2{$feb} net.mar $total1{$mar} $total2{$mar} . . . net.dec $total1{$dec} $total2{$dec} totals (sum-of-above) (sum-of-above) the other way of looking at it, is given an associative array with a number of items in it, how do I get it to print in the desired order (which is NOT alphabetical) ? any help would be appreciated and I'll share the real code via e-mail if it'll help... Thanks... -- ---------------------------------------------------------------- Vince Skahan vince@atc.boeing.com ...uw-beaver!bcsaic!vince (place in heaven assured...I finished my 3-yr-old's swing set)