Home > Archive > PERL Beginners > March 2004 > Sorting Problems
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]
|
|
| Nigel Peck - Mis Web Design 2004-03-30, 12:33 pm |
| Hi all,
I'm sure I'm just being stupid here but I can't see where:
I have an array of hash references that I'm trying to sort by one of the
key/value pairs in the hashes (see code below).
I get various errors, the current one being:
Can't coerce array into hash at
/web/secure.miswebdesign.com.on-water/cgi-bin/manager.pl line 2392.
Line 2392 is the line where the sortfunc function is defined.
Thanks in advance for much needed help :)
Cheers,
Nigel
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
my @boat_list;
foreach my $boat ( $boats->findnodes("//boat") ) {
my $section;
foreach my $ab ( $boat->findnodes("section") ) {
($section = $ab) if ($ab->getAttribute("title") eq "General Details");
}
my $boat_details = {};
$boat_details->{num} = $boat->getAttribute("num");
$boat_details->{name} = $section->findnodes("name")->[0]->firstChild->data;
$boat_details->{type} = $boat_type->getAttribute("title");
$boat_details->{price} =
$section->findnodes("price")->[0]->firstChild->data;
push @boat_list, $boat_details;
}
sub sortfunc { $a->{name} cmp $b->{name}; }
my @sorted_boats = sort sortfunc @boat_list;
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
| |
| R. Joseph Newton 2004-03-30, 9:32 pm |
| Nigel Peck - MIS Web Design wrote:
> Hi all,
>
> I'm sure I'm just being stupid here but I can't see where:
>
> I have an array of hash references that I'm trying to sort by one of the
> key/value pairs in the hashes (see code below).
>
> I get various errors, the current one being:
>
> Can't coerce array into hash at
> /web/secure.miswebdesign.com.on-water/cgi-bin/manager.pl line 2392.
>
> Line 2392 is the line where the sortfunc function is defined.
>
> Thanks in advance for much needed help :)
>
> Cheers,
> Nigel
>
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> my @boat_list;
>
> foreach my $boat ( $boats->findnodes("//boat") ) {
> my $section;
> foreach my $ab ( $boat->findnodes("section") ) {
> ($section = $ab) if ($ab->getAttribute("title") eq "General Details");
> }
>
> my $boat_details = {};
> $boat_details->{num} = $boat->getAttribute("num");
> $boat_details->{name} = $section->findnodes("name")->[0]->firstChild->data;
> $boat_details->{type} = $boat_type->getAttribute("title");
> $boat_details->{price} =
> $section->findnodes("price")->[0]->firstChild->data;
> push @boat_list, $boat_details;
> }
>
> sub sortfunc { $a->{name} cmp $b->{name}; }
> my @sorted_boats = sort sortfunc @boat_list;
Try
my @sorted_boats = sort {sortfunc($a, $b)} @boat_list;
Joseph
|
|
|
|
|