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/

In reply to Sorting sub pains... by neuroball

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.