I'm trying to solve another problem in Intermediate Perl.  Here is the
problem:
The program from Exercise 2 in Chapter  5 needs to read the entire
datafile each time it runs.  However, the Professor has a new router
logfile each day and doesn't want to keep all that data in one giant
file that takes longer and longer to process.

Fix up that program to keep the running totals in a datafile so the
Professor can simply run it on each day's logs to get the new totals.

Here is the program from Exercise 2 in Chapter 5:

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

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}} keys %{$total_bytes{$source}};
    print "$source: $total_bytes{$source}{$all} total bytes sent\n";
    for my $destination (@destinations) {
        next if $destination eq $all;
        print " $source => $destination:", " $total_bytes{$source}
{$destination} bytes\n";
    }
    print "\n";
}


In Exercise 2 in Chapter 5, I was given a text file called
coconet.dat.  Here is a sample of the text in the file:

# If you analyze and sort this data correctly, the fourth line of the
# fourth group should show these machines and total:
#
#      maryann.girl.hut => thurston.howell.hut: 123456 bytes
#
gilligan.crew.hut lovey.howell.hut 4721
thurston.howell.hut lovey.howell.hut 4046
professor.hut ginger.girl.hut 5768
gilligan.crew.hut laser3.copyroom.hut 9352
gilligan.crew.hut maryann.girl.hut 1180
fileserver.copyroom.hut thurston.howell.hut 2548
skipper.crew.hut gilligan.crew.hut 1259
fileserver.copyroom.hut maryann.girl.hut 248
fileserver.copyroom.hut maryann.girl.hut 798

Basically I passed the file to the script on the command line and it
printed the amount of data that was passed in the network in
descending order:

lovey.howell.hut: 1139833 total bytes sent
 lovey.howell.hut => laser3.copyroom.hut: 212456 bytes
 lovey.howell.hut => professor.hut: 175065 bytes
 lovey.howell.hut => ginger.girl.hut: 158084 bytes
 lovey.howell.hut => maryann.girl.hut: 155080 bytes
 lovey.howell.hut => thurston.howell.hut: 141790 bytes
 lovey.howell.hut => skipper.crew.hut: 119766 bytes
 lovey.howell.hut => fileserver.copyroom.hut: 98926 bytes
 lovey.howell.hut => gilligan.crew.hut: 78666 bytes

I'm lost how to modify the program to solve the problem.  I did modify
the script with the Storable module:

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

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;
}

store \%total_bytes, 'storage';

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}} keys %{$total_bytes{$source}};
    print "$source: $total_bytes{$source}{$all} total bytes sent\n";
    for my $destination (@destinations) {
        next if $destination eq $all;
        print " $source => $destination:", " $total_bytes{$source}
{$destination} bytes\n";
    }
    print "\n";
}

Basically I'm not sure how to combine the totals from previous outputs
with new files.  For example, on Monday, I received a log file.  I
used my script to store the totals in a file called storage.  If I
received a new log file on Tuesday, how can I combine the new data
with data stored in storage?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to