Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!snorkelwacker!apple!oliveb!orc!mipos3!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: sort Keywords: sort Message-ID: <1990Feb1.163539.10576@iwarp.intel.com> Date: 1 Feb 90 16:35:39 GMT References: <1990Feb1.050145.21183@athena.mit.edu> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: usa Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 55 In-Reply-To: ccount@athena.mit.edu (Craig A Counterman) In article <1990Feb1.050145.21183@athena.mit.edu>, ccount@athena (Craig A Counterman) writes: | I am a convert to perl, thanks to the Perl Reference Guide, and a2p | and s2p. I used the programs to convert a series of awk and sed | scripts to perl, and the ref. card to understand the code and fine | tune the result. It works great. | | One thing I'd like to do but don't yet see how (if it's possible), is | to sort the lines of a file based on a given field. My actual | application would be a bit more complex, but that's the basic idea. | | sort() would sort a single array, but I think I need it to also | generate an index array I could then use to reference the other arrays. | (I'd read and split each line in the file into a series of arrays, one | column per array and visa versa, do some processing, and then want to | sort on one of the arrays). Ahh yes, but what's *in* a single array?! All sorts of things (pun intended). If your field is defined by fixed character position, say, the 14th through 20th characters of the line, try this: @whole = ; # snarf everything in sub byfield14to20 { substr($a, 13, 7) > substr($b, 13, 7); } print sort byfield14to20 @whole; If your field is defined by separators, it's a little more difficult. You can either split each line each time you test it (bad for lotsa data), or split it once and cache it, then sort an "indexing" array, like so (presuming you want to sort on the fourth field): @whole = ; for (@whole) { @x = split; push(@f4, $x[3]); } # now $f4[n] is the fourth field of $whole[n] sub byfourthfield { $f4[$a] > $f4[$b]; } @indices = sort byfourthfield 0..$#whole; # now @indicies has the pointers into @whole for the sorted array for (@indicies) { print $whole[$_]; } # larry? is that the same as 'print @whole[@indices];'? A little tricky, but if you take it through step-by-step, you'll see what I'm doing there. If you get stuck, write me back, and I'll do it in detail. Just another Perl hacker, of sorts, -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/