Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!comp.vuw.ac.nz!ajv From: Andrew.Vignaux@comp.vuw.ac.nz (Andrew Vignaux) Newsgroups: comp.lang.perl Subject: Re: sort Message-ID: <1990Sep01.001741.17511@comp.vuw.ac.nz> Date: 1 Sep 90 00:17:41 GMT References: <1990Aug21.224327.20194@iwarp.intel.com> <1990Aug22.111018.3329@comp.vuw.ac.nz> <105536@convex.convex.com> <1990Aug29.191454.23527@iwarp.intel.com> <9337@jpl-devvax.JPL.NASA.GOV> Sender: news@comp.vuw.ac.nz (News Admin) Organization: Comp Sci, Victoria University, Wellington, New Zealand. Lines: 55 In article <1990Aug29.191454.23527@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes: : In article <105536@convex.convex.com>, tchrist@convex (Tom Christiansen) writes: : | Is this simple enough for the sort function? : | : | sub bynum { substr($a,$[+1,10) > substr($b,$[+1,10); } : : Nope nope nope. I made that same mistake once. Think about : what this returns... either "1" or "0", not "1" or "-1". Arrrgh. :-) Actually, the Berkeley qsort (well let's be precise -- the qsort on our MORE/bsd 4.3+ boxes running on hp300s) still manages to sort if you use this comparison routine! In article <9337@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: > I just had the weirdest thought. The ne and != operators should maybe > return -1 or +1 when the operands aren't equal. > > Larry This is a great idea! In conjunction with || returning the last value evaluated means sort functions can turn into: sub sort_it { $key1{$a} ne $key1{$b} || -(&fun2{$a} != &fun2{$b}); } where &fun2() can return floats! Unfortunately, it will break some of my scripts. E.g if (($sort_rank != 0) + ($sort_articles != 0) + ($sort_size != 0) > 1) { print STDERR "$program: only one of -r, -a, -s should be supplied\n"; exit (1); } where $sort_* could be -1, 0, +1. But I can fix those. Possible problems: + it looks strange. Take another look at sort_it! Does it sort things the right way? + people will have to re-think how to use comparison operators because "!=" does not mean "!( == )" E.g negating an expression (ala De Morgan) will take some thought + -(a != b) <=> (-a != -b) which is new! + it'll screw up my C programming :-) I realise it isn't a democracy, but you've got my vote. Andrew -- Domain address: Andrew.Vignaux@comp.vuw.ac.nz