This came over the MacPerl list recently: At 09:21 -0400 6/16/99, he-who-shall-be-nameless wrote: > I am trying to sort a simple text file: > > Janssen||Willem > Hinders||Trintje > Beving||John > Beving||Henrietta > Beving||Ubbe > Beving||Henry > Beving||Henry > Beving||Etta > Beving||Minnie > Beving||Bertha > Willemssen||male > Willemssen||male > Willemssen||Wopka(e) > Brower||Coba > Brower||John > Brower||Bertha > > I'm using the following code and it does not seem to sort them. > > if(open(INFILE,"names.txt") || die("Cannot open names.txt")) { > while (<INFILE>) { > @fields = $_; > foreach $field (@fields) { > @array = split /\|\|/, $field; > @array = sort(@array); > chomp($array[0],$array[1]); > $lastname = $array[0]; > $firstname = $array[1]; > print "$lastname, $firstname\n"; > } > } > close(INFILE); > } > > It does not seem to work. Is there something I'm missing in the code? >From there, the discussion went off why the poster's code didn't work, and thence into Schwartzian Transforms and fancy variations thereof: > open(INFILE,"names.txt") || die("Cannot open names.txt: $!\n"); > @sorted = > map { "$_->[0], $_->[1]" } > sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } > map { chomp; [ split /\|\|/ ] } <INFILE>; > $, = "\n"; > print @sorted; > close(INFILE); And Much Discussion ensued. I looked at the data. I looked at the (assumed) required output (a simple sorted list, supposedly). And I thought: K.I.S.S. My entry into the discussion: #!perl open(IN, "names.txt") or die "Oops"; @names = <IN>; # note this is an ASCII (case-sensitive) sort; you may want to # "improve" on that @names = sort @names; foreach (@names) { print; # note you could now split or substitute ", " for the || or... } My solution seemed _so_ simple I was wondering if I'd just missed something about the original problem. Or did the others not see the tree for the forest? Did I achieve the Great Aha!? I ran this past MJD and Randal before deciding to send it to FWP. And Merlin out-Aha'd me! At 13:06 -0700 6/17/99, Randal L. Schwartz wrote: > @ARGV = qw(names.txt); > print sort <>; # would have worked as well ooooooooooooooooooh 8*) - Vicki -- -- |\ _,,,---,,_ Vicki Brown <vlb@cfcl.com> ZZZzz /,`.-'`' -. ;-;;,_ Journeyman Sourceror: Scripts & Philtres |,4- ) )-,_. ,\ ( `'-' P.O. Box 1269 San Bruno CA 94066 '---''(_/--' `-'\_) http://www.cfcl.com/~vlb http://www.macperl.org ==== Want to unsubscribe from this list? (Don't you love us anymore?) ==== Well, if you insist... Send mail with body "unsubscribe" to ==== fwp-request@technofile.org