neuroball has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting sub pains...
by tcf22 (Priest) on Oct 21, 2003 at 23:51 UTC | |
|
•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 |