an_mo has asked for the wisdom of the Perl Monks concerning the following question:

I am having problem with the following codes. The second time I call &printcells (line A) the hash does not result being printed properly sorted by value as I would like. %AnotherHash is printed sorted by the values of %ThisHash Any idea what is wrong?
sub GetReferersBar{ my ($key,$value,%thishash,$bartext); my ($i)==0; my ($nrefs,$n1,$n2); sub by_value { $a <=> $b }; sub printcells { my ($maxcols,$reference_to_values)=@_ ; my ($i,$html); my %thishash = %{$reference_to_values}; sub by_nhits { $thishash{$b}<=>$thishash{$a} }; foreach $key (sort by_nhits keys %thishash) { $html .= "<TR><TD>$thishash{$key}" } return $html } $bartext = "<TABLE>"; $bartext .= &printcells($nrefs,\%ThisHash); $bartext .= &printcells($nrefs,\%AnotherHash); # LINE A <-- probl +em $bartext .= "</TABLE>" ; return $bartext; }

Replies are listed 'Best First'.
Re: problem in sorting has by value
by runrig (Abbot) on Dec 11, 2001 at 05:16 UTC
    use strict and warnings. And don't nest your subroutines like that unless you know what you're doing and want to create closures on purpose. You might be using strict (I can't absolutely tell), but you're not using warnings, because you have a problem with that $key variable in the foreach loop not staying shared (its declared in a sub in an outer scope but being used in a sub in an inner scops).
      you're right I am using strict, but not warnings (dunno how). If you have time could you elaborate your comment about outer/inner scopes?
Re: problem in sorting has by value
by dragonchild (Archbishop) on Dec 11, 2001 at 19:59 UTC
    You've got one major problem with that code, as written, as well as a number of style issues, which can cause further problems.
    sub GetReferersBar{ my ($key,$value,%thishash,$bartext); # This line simply asserts that $i, when first declared and # converted to a number is zero. my ($i)==0; my ($nrefs,$n1,$n2); sub by_value { $a <=> $b }; sub printcells { my ($maxcols,$reference_to_values)=@_ ; my ($i,$html); my %thishash = %{$reference_to_values}; sub by_nhits { $thishash{$b}<=>$thishash{$a} }; foreach $key (sort by_nhits keys %thishash) { $html .= "<TR><TD>$thishash{$key}" } return $html } $bartext = "<TABLE>"; $bartext .= &printcells($nrefs,\%ThisHash); $bartext .= &printcells($nrefs,\%AnotherHash); # LINE A <-- probl +em $bartext .= "</TABLE>" ; return $bartext; }
    The style issues are too numerous to state in that snippet. You're an ANSI C programmer by training, aren't you? :-)

    Try doing something like this:

    sub printcells { my $href = shift; my $html; $html .= "<TR><TD>$thishash{$_}</TD></TR>" foreach sort { $href->{$b} <=> $href->{$a} } keys %$thishash; return $html } sub GetReferersBar { my ($thisHash, $anotherHash) = @_; my $bartext = "<TABLE>"; $bartext .= printcells($thisHash); $bartext .= printcells($anotherHash); $bartext .= "</TABLE>" ; return $bartext; }
    Notice the difference? Now, you have to pass in the references to %ThisHash and %AnotherHash. All the unused variables are removed and the useless creation of the sub to sort a specific situation is removed, instead showing that the sort is specific to that spot.

    Now, if you want to have by_nhits() be in a module, I would suggest rethinking how it should work. All you're doing is a reverse numeric sort on the values of the hashes you're working with. Why not just do a reverse numeric sort on the values, maybe like this:

    foreach my $key (map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, $thishash->{$_} ] } keys %$thishash;
    This is known as the Schwartzian Transform. You create a listref of (generally) two elements. The first element(s) is the one you care about and the second element(s) is the one you want to sort on. You sort on the second element(s) and give back the first element(s). This way, you can create a ST_by_reverse_numeric() that's generic now. If you were to put it in a module, it could look something like this:
    sub ST_by_reverse_numeric { local ($a, $b); $b->[1] <=> $a->[1] }
    The locals are to allow for the import of the sorting function. If you really want to know why that is needed, look around the monastery. We've discussed at least twice in the last month.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      foreach my $key (map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, $thishash->{$_} ] } keys %$thishash;
      If the sort field is just a hash key lookup, its not worthwhile to do the Schwartzian thing (even with the hash dereference). You have to build an array reference, and then dereference it, so its only worth it for more expensive operations. Just do a straight sort:<code> sort { $thishash->{$b} <=> $thishash->{$a} } keys %thishash;
        I agree that doing a straight sort in this case is more efficient, but if the user wanted to use pre-defined sorting function, this is what would have to be done.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      The style issues are too numerous to state in that snippet. You're an ANSI C programmer by training, aren't you? :-)

      I wish I was. I am only an amateur who learnt perl reading the lama book in 2 days (starting 3 days ago). Thanks for the reply though. I'll try to avoid writing subs inside subs.

Re: problem in sorting has by value
by an_mo (Initiate) on Dec 11, 2001 at 05:09 UTC
    well it turns out that the following works: replace the foreach line above with
    foreach $key (sort {$thishash{b}<=>$thishash{a} } keys %thishash)
    now the question is: why wasn't the sub by_nhits working before?
      Um, wouldn't that be $a and $b as keys instead of just a and b?

          -- Chip Salzenberg, Free-Floating Agent of Chaos