For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Sorting a list of names by last name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting a list of names by last name
extraspecialbitter

2006-11-20, 6:58 pm

I'm not a total Perl newbie, but admittedly I haven't progressed much
beyond the "Hello World" phase. I'm now trying to figure out how to
tackle what seems to be a fairly simple problem: sorting through a list
of space-separated names by last name. The main wrinkle is that middle
names and initials are allowed but used less than half the time.
Sorting by the second field will therefore catch most but certainly not
all surnames.

One approach is to read each name into an array and then pop the last
element of the array. I'm trying to figure out how to populate the
array without quoting the raw data and separating the fields with
commas.

Any tips would be sincerely appreciated.

- Paul (Extra Special Bitter)

DJ Stunks

2006-11-20, 6:58 pm


extraspecialbitter wrote:
> I'm not a total Perl newbie, but admittedly I haven't progressed much
> beyond the "Hello World" phase. I'm now trying to figure out how to
> tackle what seems to be a fairly simple problem: sorting through a list
> of space-separated names by last name. The main wrinkle is that middle
> names and initials are allowed but used less than half the time.
> Sorting by the second field will therefore catch most but certainly not
> all surnames.


sort by the last name by using the -1 index

-jp

extraspecialbitter

2006-11-20, 6:58 pm

jp - thanks for the reply.

do you know how I would initialize the array? due to the fact that the
names are space-delimited, they appear to populate the array as a
one-dimensional text string.

On Nov 20, 4:21 pm, "DJ Stunks" <DJStu...@gmail.com> wrote:
> extraspecialbitter wrote:
>
> sort by the last name by using the -1 index
>
> -jp


DJ Stunks

2006-11-20, 6:58 pm


extraspecialbitter wrote:
> On Nov 20, 4:21 pm, "DJ Stunks" <DJStu...@gmail.com> wrote:
>
> do you know how I would initialize the array? due to the fact that the
> names are space-delimited, they appear to populate the array as a
> one-dimensional text string.



Please don't top post, quote some context and post your reply
underneath.

Here's a framework which does what you want:

#!/usr/bin/perl

use strict;
use warnings;

my @names = (
'Fred Flintstone',
'Barney Rubble',
'George Nate Slate',
'Great Gazoo',
);

$, = "\n";
print sort by_last_name @names;

sub by_last_name {
return (split ' ', $a )[-1]
cmp
(split ' ', $b )[-1];
}

__END__

-jp

DJ Stunks

2006-11-20, 6:58 pm


extraspecialbitter wrote:
> I'm not a total Perl newbie, but admittedly I haven't progressed much
> beyond the "Hello World" phase. I'm now trying to figure out how to
> tackle what seems to be a fairly simple problem: sorting through a list
> of space-separated names by last name. The main wrinkle is that middle
> names and initials are allowed but used less than half the time.
> Sorting by the second field will therefore catch most but certainly not
> all surnames.


sort by the last name by using the -1 index

-jp

extraspecialbitter

2006-11-20, 6:58 pm

On Nov 20, 5:05 pm, "DJ Stunks" <DJStu...@gmail.com> wrote:

> Here's a framework which does what you want:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @names = (
> 'Fred Flintstone',
> 'Barney Rubble',
> 'George Nate Slate',
> 'Great Gazoo',
> );
>
> $, = "\n";
> print sort by_last_name @names;
>
> sub by_last_name {
> return (split ' ', $a )[-1]
> cmp
> (split ' ', $b )[-1];
> }
>
> __END__
>
> -jp


That worked perfectly. Thanks!

Paul (Extra Special Bitter)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com