in reply to Re^2: How do I combine SPLIT with trimming white space
in thread How do I combine SPLIT with trimming white space
I thought something was weird with the missing semicolon - just a small detail.
As far as sorting goes print "$name\n"; #push to DB or whatever here... is where you can just push to a list like @full_names. Here is one way of many to do the sort. Note that the "Boop" family winds up in the correct sort order.
I added the simple code to sort by first name.#!/usr/bin/perl -w use strict; my @names = ( "Builder,Bob", "Stein,Franklin", "Boop,Elizabeth", "Boop,Albert", "Bear,Izzy", "SomeGuy,Guy", "Einstein,Albert",); @names = sort by_last_name @names; print join("\n",@names),"\n"; sub by_last_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_last cmp $b_last or $a_first cmp $b_first } __END__ Prints: Bear,Izzy Boop,Albert Boop,Elizabeth Builder,Bob Einstein,Albert SomeGuy,Guy Stein,Franklin
#!/usr/bin/perl -w use strict; my @names = ( "Builder,Bob", "Stein,Franklin", "Boop,Elizabeth", "Boop,Albert", "Bear,Izzy", "SomeGuy,Guy", "Einstein,Albert",); @names = sort by_last_name @names; print join("\n",@names),"\n"; ## Now print sorted by first name ## print "\nNow sorted by first name\n"; @names = sort by_first_name @names; foreach my $full_name (@names) { my ($last,$first) = split (/,/,$full_name); print "$first,$last\n"; } sub by_last_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_last cmp $b_last or $a_first cmp $b_first } sub by_first_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_first cmp $b_first or $a_last cmp $b_last } __END__ Prints: Bear,Izzy Boop,Albert Boop,Elizabeth Builder,Bob Einstein,Albert SomeGuy,Guy Stein,Franklin Now sorted by first name Albert,Boop Albert,Einstein Bob,Builder Elizabeth,Boop Franklin,Stein Guy,SomeGuy Izzy,Bear
|
---|