home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6964 < prev    next >
Encoding:
Internet Message Format  |  1992-11-12  |  1.3 KB

  1. Path: sparky!uunet!know!mips2!news.bbn.com!noc.near.net!nic.umass.edu!caen!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!sgigate!sgi!wdl1!wdl39!mab
  2. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: sorting problem
  5. Keywords: sort
  6. Message-ID: <1992Nov12.224027.3713@wdl.loral.com>
  7. Date: 12 Nov 92 22:40:27 GMT
  8. References: <BxM5w2.Iyr@immd4.informatik.uni-erlangen.de>
  9. Sender: news@wdl.loral.com
  10. Organization: Loral Western Development Labs
  11. Lines: 33
  12.  
  13. In article <BxM5w2.Iyr@immd4.informatik.uni-erlangen.de> mnrausch@cip.informatik.uni-erlangen.de (Martin Rausche) writes:
  14. >I need a fast sorting routine for the following problem:
  15. >I have a database and the entries look like this:
  16. >keyword;author;title;owner
  17. >My solution:
  18. >sub byname
  19. >{
  20. >        @ah=split(/;/,$a);
  21. >        @be=split(/;/,$b);
  22. >        shift(@ah);
  23. >        shift(@be);
  24. >        $ah=join(";",@ah);
  25. >        $be=join(";",@be);
  26. >        $ah cmp $be;
  27. >}
  28. >@data=sort byname @data;
  29.  
  30. split take a third parameter that specifies how many item to slipt into.
  31. This can be used to avoid the joins and shifts like so:
  32.  
  33. sub byname {
  34.     @ah=split(/;/,$a,2);
  35.     @be=split(/;/,$b,2);
  36.     $ah[1] cmp $be[1];
  37. }
  38.  
  39. The splits also get speed up because they can quit as soon as the first ';'
  40. is seen.
  41.  
  42. --
  43. Perl's Maternal Uncle
  44. Mark Biggar
  45. mab@wdl1.wdl.loral.com
  46.