For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > sorting array full of hash references









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 array full of hash references
Jeremy Kister

2005-06-08, 3:57 am

I'm stumped on how to sort an array based on a hash refrences's key in
each element of my array.

this is dumbed down code of what I have:
my @array;
while(my $row = $sth->fetchrow_arrayref){
my %hash = (id => $row->[0], name => $row->[1]);
push(@array, \%hash);
}

after the while loop, I'm trying to build a new version of @array,
sorted by the 'name' key.

Anyone feel like throwing example code my way?



--

Jeremy Kister
http://jeremy.kister.net./
John W. Krahn

2005-06-08, 3:57 am

Jeremy Kister wrote:
> I'm stumped on how to sort an array based on a hash refrences's key in
> each element of my array.
>
> this is dumbed down code of what I have:
> my @array;
> while(my $row = $sth->fetchrow_arrayref){
> my %hash = (id => $row->[0], name => $row->[1]);
> push(@array, \%hash);
> }
>
> after the while loop, I'm trying to build a new version of @array,
> sorted by the 'name' key.
>
> Anyone feel like throwing example code my way?


my @sorted = sort { $a->{ name } cmp $b->{ name } } @array;


John
--
use Perl;
program
fulfillment
Peter Rabbitson

2005-06-08, 3:57 am

On Tue, Jun 07, 2005 at 10:40:43PM -0400, Jeremy Kister wrote:
> I'm stumped on how to sort an array based on a hash refrences's key in
> each element of my array.
>
> this is dumbed down code of what I have:
> my @array;
> while(my $row = $sth->fetchrow_arrayref){
> my %hash = (id => $row->[0], name => $row->[1]);
> push(@array, \%hash);
> }
>
> after the while loop, I'm trying to build a new version of @array,
> sorted by the 'name' key.
>


Sort as its first argument can take a code block with the two
special variables $a and $b passed to this block for each iteration of the
sort algorithm. The return values must must be one of -1 0 or 1, which
have the same meaning as in the cmp and <=> comapriosn operators. In other
words in you case you want to do:

@array = sort { $a->{name} cmp $b->{name} } @array;

I hope the dereferencing above is pretty self explanatory.
Try perldoc -f sort for more info - there are some pretty nifty examples in
there.

Peter
Charles K. Clarkson

2005-06-08, 3:57 am

Jeremy Kister <mailto:perl-beginners-02@jeremykister.com> wrote:

: I'm stumped on how to sort an array based on a hash refrences's
: key in each element of my array.
:
: this is dumbed down code of what I have:
: my @array;
: while(my $row = $sth->fetchrow_arrayref){

: my %hash = (id => $row->[0], name => $row->[1]);
: push(@array, \%hash);

No need for %hash.

push @array, {
id => $row->[0],
name => $row->[1],
};


: }
:
: after the while loop, I'm trying to build a new version of
: @array, sorted by the 'name' key.

This might do.

my @sorted_array;
if ( @array) {
@sorted_array = sort { $a->{name} cmp $b->{name} } @array;

} else {
# Houston, we have a problem.
}


Q: Why use fetchrow_arrayref()? In fact, why sort with perl?

If you structure your query to return the first column "AS id"
the second column "AS name", with an "ORDER BY name" clause, you
could just use this to build the sorted array.

my @array;
while ( my $row = $sth->fetchrow_hashref() ) {
push @array, $row;
}


HTH,


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Sponsored Links







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

Copyright 2008 codecomments.com