in reply to Re: Sort function issue
in thread Sort function issue

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

Replies are listed 'Best First'.
Re: Re: Re: Sort function issue
by tall_man (Parson) on Jun 24, 2003 at 19:03 UTC
    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