CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sorting Output

 



d1zz13
User

Jun 21, 2001, 9:11 AM

Post #1 of 5 (1268 views)
Sorting Output Can't Post

Hi there,

I have the following code that outputs a list of names to my browser:

Code
foreach $person(@user_details){ 
($user,$pass,$firstname,$surname) = split /\|/,$person;
$surname = uc($surname);
$firstname = lc($firstname);
$firstname = ucfirst($firstname);
print "$surname $firstname\n";
}

It produces the names in the following format:

Code
SMITH John 
BLOGGS Joe

How can I sort it so that it shows BLOGGS before it shows SMITH?

Regards and TIA
Rich



rGeoffrey
User / Moderator

Jun 21, 2001, 9:43 AM

Post #2 of 5 (1264 views)
Re: Sorting Output [In reply to] Can't Post

This will work...


Code
my @data = ( 
'a|b|marge|simpSON',
'a|b|hOmeR|simpSON',
'a|b|Fred|Flintstone',
'a|b|Barney|rubble',
'a|b|geoRge|jeTSOn',
);

my @sorted = sort map {
my ($first, $last) = (split /\|/)[2,3];
join (' ', uc ($last), ucfirst (lc ($first)))
} @data;

my @hardway;

foreach my $line (@data) {
my ($user, $pass, $first, $last) = split (/\|/, $line);
$last = uc ($last);
$first = ucfirst (lc ($first));
push (@hardway, join (' ', $last, $first));
}

print (join ("\n", @sorted),
"\n----------------------------------------------\n",
join ("\n", sort @hardway),
"\n");

I have included both the short map version and the longer but easier to understand foreach version.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


mhx
Enthusiast / Moderator

Jun 21, 2001, 9:48 AM

Post #3 of 5 (1264 views)
Re: Sorting Output [In reply to] Can't Post

Hi Rich,

I'd put the data into a hash table first. I'm sure there's a direct way to do it, but I think this is a relatively clean solution:

Code
#!/perl/bin -w 
use strict;

my @user_details = (
'jsmith|0815|john|smith',
'jbloggs|4711|joe|bloggs',
);

foreach( @user_details ) {
my ($user,$pass,$firstname,$surname) = split /\|/;
$_ = {
user => $user,
pass => $pass,
firstname => ucfirst lc $firstname,
surname => uc $surname,
}
}

foreach( sort { $a->{surname} cmp $b->{surname} } @user_details ) {
print "$_->{surname} $_->{firstname}\n";
}

The first loop prepares the strings and stores it all in hash tables, the references to these are stored in the @user_details array. The second loop generates the sorted output.
Hope this helps, if you got questions, ask!

-- Marcus

P.S.: rGeoffrey rushed in with his answer just as I was editing mine Smile. Take whichever code you can easily understand.

P.P.S.: I think rGeoffrey's map version is faster & shorter & better suited if you just want to create your output. If you want to 'work' with the data beyond this, I think it's easier if you got the data in a hash.

(This post was edited by mhx on Jun 21, 2001, 8:57 AM)


rGeoffrey
User / Moderator

Jun 21, 2001, 11:11 AM

Post #4 of 5 (1262 views)
Re: Sorting Output [In reply to] Can't Post

I think you really wanted to say...


Code
sort { "$a->{surname} $a->{firstname}" cmp "$b->{surname} $b->{firstname}" }

Without the fisrt name my sample data will put Marge Simpson before Homer Simpson. And while she is the leader of the family, she does not come first alphabetically.

But you are right that the hash solution will make further processing easier if it is going to be happening later.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


mhx
Enthusiast / Moderator

Jun 21, 2001, 11:57 AM

Post #5 of 5 (1261 views)
Re: Sorting Output [In reply to] Can't Post

Oooo yes! As I was riding back home from work exactly this 'mistake' occurred to me, too. Wink
But I was rather thinking of

Code
sort { $a->{surname}   cmp $b->{surname} or 
$a->{firstname} cmp $b->{firstname} }

but I don't think there's a big difference. Perhaps the above is faster than first concatenating the strings and comparing then, but I'm not going to benchmark it...

Thanks anyway,
Marcus


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives