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: Intermediate:
Sort::Tree question

 



zatlas1
User

Jun 6, 2016, 9:07 PM

Post #1 of 3 (7706 views)
Sort::Tree question Can't Post

I have to traverse on a tree with non numerical id's. The example in Sort::Tree says

Code
    foreach my $row (Sort::Tree::tree_to_list(\@tree, 
['id'],
[\&Sort::Tree::numerically],
'class')) {
print ' ' x $row->{class}, $row->{name}, "\n";
}

When I try to replace [\&Sort::Tree::numerically] with something like [\&alpha] where alpha is

sub alpha {{lc($a) cmp lc($b)}};

I get that $a and $b are not initialized and the package resorts to the default 'numerically' that obviously fails. Does anybody have any experience with that package and may shed light on how to use it.

Thank you
ZA


Laurent_R
Veteran / Moderator

Jun 7, 2016, 1:21 AM

Post #2 of 3 (7704 views)
Re: [zatlas1] Sort::Tree question [In reply to] Can't Post

Well, we would need to know more about your tree to be able to suggest a solution and test it.

Maybe you could try this:


Code
sub alpha { 
my ($a, $b, $f) = @_;
lc($a->{$f}) cmp lc($b->{$f});
}


Or try to use the "alphabetically" subroutine provided by the module:


Code
    foreach my $row (Sort::Tree::tree_to_list(\@tree,  
['id'],
[\&Sort::Tree::alphabetically],
'class')) {
print ' ' x $row->{class}, $row->{name}, "\n";
}



zatlas1
User

Jun 7, 2016, 6:44 AM

Post #3 of 3 (7691 views)
Re: [Laurent_R] Sort::Tree question [In reply to] Can't Post

Thank you
alphabetically won't cut it for me since I need to do some odd manipulation in the sort routine but the other suggestion was perfect. Modified it to my needs and it works.

ZA

 
 


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

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