Home: Perl Programming Help: Intermediate:
Sort::Tree question



zatlas1
User

Jun 6, 2016, 9:07 PM


Views: 8810
Sort::Tree question

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


Views: 8808
Re: [zatlas1] Sort::Tree question

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


Views: 8795
Re: [Laurent_R] Sort::Tree question

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