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

Dear Co-Monks,

I did hit a brick wall recently when I was working through Learning Perl Objects, References & Modules and wanted to use sort in a sub.

In chapter 4 the exercise was to sort a hash with network traffic data twice... which shouldn't be a big problem.

I tried to write the code with a sort sub and failed miserably... The code:

#!/usr/local/bin/perl use warnings; use diagnostics; use strict; use Data::Dumper; my %data; my $data_file = '/users/oliversti/documents/development/perl/a +lpaca book/alpaca_files/ch04/coconet.dat'; my $all = '** all machines combined **'; open FH, "$data_file" or die "Couldn't open $data_file: $!\n"; while (<FH>) { next if (/^#/); my ($source, $destination, $bytes) = split; $data{$source}{$destination} += $bytes; $data{$source}{$all} += $bytes; } close FH or die "Couldn't close $data_file: $!\n"; sub traffic_combined { $data{$b}{$all} <=> $data{$a}{$all} } for my $source (sort traffic_combined keys %data) { print "$source data transfer out: $data{$source}{$all}.\n" +; sub traffic_machine { $data{$source}{$b} <=> $data{$source}{$a} } for my $destination (sort traffic_machine keys %{$data{$so +urce}}) { next if ($destination eq $all); print "$source -> $destination: $data{$source}{$destin +ation}.\n"; } }

I couldn't understand why the second sort ('traffic_machine') didn't work. I debugged the script and could print the values of $a and $b. So everything seemed fine... until it hit me: By using a sub the %data and $source variables were out of scope.

After trying to pass the hash and scalar into the sub, which failed miserably I came up with two solutions. Or better said a hack and a solution.

The hack: Make the variables that I want to access global by using OUR.

The solution: Using the code block of the subs in-line as shown below:

for my $source (sort {$data{$b}{$all} <=> $data{$a}{$all}} key +s %data) { print "$source data transfer out: $data{$source}{$all}.\n" +; for my $destination (sort {$data{$source}{$b} <=> $data{$s +ource}{$a}} keys %{$data{$source}}) { next if ($destination eq $all); print "$source -> $destination: $data{$source}{$destin +ation}.\n"; } }

My final questions to the monks is: Is there a way that I overlooked or don't know that allows me to pass variables (next to $a and $b) into a sort sub?

thanks
/oliver/

Replies are listed 'Best First'.
Re: Sorting sub pains...
by tcf22 (Priest) on Oct 21, 2003 at 23:51 UTC
    You have to prototype the sort function and it will pass them into the function.
    my @sorted_array = sort sort_func @array; sub sort_func($$){ my $var1 = $_[0]; my $var2 = $_[1]; return $var2 <=> $var1; }
    but it sounds like you need the hash ref also, so you can use the sort BLOCK ARRAY construct, but doing it inline makes the most sense.
    Example:
    my @sorted_array = sort {&sort_func($::a, $::b, $source, $data)} @arra +y; sub sort_func(){ my $var1 = $_[0]; my $var2 = $_[1]; my $source = $_[2]; my $data = $_[3]; return $data{$source}{$var2} <=> $data{$source}{$var1}; }

    - Tom

•Re: Sorting sub pains...
by merlyn (Sage) on Oct 21, 2003 at 23:11 UTC
Re: Sorting sub pains...
by dragonchild (Archbishop) on Oct 22, 2003 at 13:42 UTC
    You had another problem (besides the ones already mentioned) - unlike PASCAL, subroutines are not (naturally) lexically scoped. You can lexically scope them, but it looks (and feels) different. Try the following:
    for my $source (sort traffic_combined keys %data) { print "$source data transfer out: $data{$source}{$all}.\n" +; my $traffic_machine = sub { $data{$source}{$b} <=> $data{$source}{$a} } for my $destination (sort $traffic_machine keys %{$data{$s +ource}}) { next if ($destination eq $all); print "$source -> $destination: $data{$source}{$destin +ation}.\n" +; } }

    That way, we're making the inner sort into a closure, which will keep track of the current $source. sort checks its first argument to see if it's a sub or a closure, making this useful.

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

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.