Thomas H. George wrote:
The solution given on page 225 fails if warn or use strict are turned
on.  The error is the use of an undefined variable in the sort of
destinations.

I decided to ignore this and move on to Chapter 6. There using perldebug
and -d:ptkdb I have studied the problem again.  The hashes all look ok
but something related to the $all variable introduces undefined
variables with each iteration of the sort routine.

Here is the solution as I have copied it:


#!/usr/bin/perl -w
#
use strict;
use diagnostics;

my %total_bytes;
my $all = "All Machines";
while ( <> ) {
    next if  /^#/;
my ($source, $destination, $bytes) = split; $total_bytes{$source}{$destination} += $bytes;
    $total_bytes{$source}{$all} += $bytes;
        
}
my @sources =
    sort { $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all} }
    keys %total_bytes;

for my $source (@sources) {
    my @destinations =
        sort {$total_bytes{$source}{$b} <=> $total_bytes{$source}{a} }
                                                                   ^^^
That should be:

         sort {$total_bytes{$source}{$b} <=> $total_bytes{$source}{$a} }


        keys %{ $total_bytes{$source} };
    print "$source: $total_bytes{$source}{$all} total bytes send\n";
    for my $destination (@destinations) {
        next if $destination eq $all;
        print "  $source => $destination:",
            " $total_bytes{$source}{$destination} bytes\n";
    }
    print "\n";
}

For the present I have turned off warn and use strict in order to
proceed with the solution of Chapter 6, Problem 1 using this program as
the starting point.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to