Sorting Problems
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; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Sorting Problems
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; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Sorting Problems
Nigel Peck - MIS Web Design [EMAIL PROTECTED] 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. : : =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= : 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); : } You really should test that $section has a value in it. : 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; It's doesn't fix the problem, but this could be re-written this way: push @boat_list, { num = $boat-getAttribute(num), name= $section-findnodes(name)-[0]-firstChild-data, type= $boat_type-getAttribute(title), price = $section-findnodes(price)-[0]-firstChild-data, }; : } : : sub sortfunc { $a-{name} cmp $b-{name}; } : my @sorted_boats = sort sortfunc @boat_list; : =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= It would appear that @boat_list has something in it other than what is expected. Try dumping @boat_list to see what is in it. TEST: #!/usr/bin/perl use strict; use warnings; use Data::Dumper 'Dumper'; my @sorted_boats = sort sortfunc ( { name = 'foo' }, { name = 'bar' }, { name = 'baz' }, ); print Dumper [EMAIL PROTECTED]; sub sortfunc { $a-{name} cmp $b-{name}; } __END__ HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting Problems
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response