in reply to Sort function issue

Why wouldn't this work:
sub by_dept_total { return $dept_n_code{$a}->{'dept'} <=> $dept_n_code{$b}->{'dept'}; } sort by_dept_total ...

Replies are listed 'Best First'.
Re: Re: Sort function issue
by artist (Parson) on Jun 24, 2003 at 18:49 UTC
    The reason could be:
    use strict; use warnings; main(); xyz(); sub main{ my $h; foreach (1..20){ $h->{$_} = int rand (100); } foreach (sort by_val keys %{$h}){ print "$_","\t",$h->{$_}; } } sub xyz { my $k; foreach (1..20){ $k->{$_} = int rand (100); } foreach (sort by_val keys %{$k}){ print "$_","\t",$k->{$_}; } } sub by_val { $h->{$a} <=> $h->{$b}; }
    artist
      In that case:
      use strict; use warnings; main(); xyz(); sub main{ my $h; foreach (1..20){ $h->{$_} = int rand (100); } my $by_val = make_sort($h); foreach (sort $by_val keys %{$h}){ print "$_","\t",$h->{$_}; } print "\n"; } sub xyz { my $k; foreach (1..20){ $k->{$_} = int rand (100); } my $by_val = make_sort($k); foreach (sort $by_val keys %{$k}){ print "$_","\t",$k->{$_}; } print "\n"; } sub make_sort { my $dat = shift; return sub { $dat->{$a} <=> $dat->{$b} }; }
        Yes that should work. Strict was the issue. In general I dont want a direct reference to the data structure being sorted, since i want to put the sort function in a required library, which has always been somewhat of an issue for me.
        I very much like this solution - thanks much