Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

updating totals
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?


Report this thread to moderator Post Follow-up to this message
Old Post
Chris
08-24-07 03:01 AM


Re: updating totals
Ok I figured out a solution:

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

my %total_bytes;
my $all = "**all machines**";

my $output_file = 'storage';
if (-e $output_file) {
my $hashref = retrieve $output_file;
%total_bytes = %$hashref;
} else {
print "$output_file has not been generated.\n"
}

#stuff hash
while (<> ) {
next if /^#/;
my ($source, $destination, $bytes) = split;
$total_bytes{$source}{$destination} += $bytes;
$total_bytes{$source}{$all} += $bytes;
}

#sort totals for $all
my @sources = sort {$total_bytes{$b}{$all} <=> $total_bytes{$a}{$all}}
keys %total_bytes;

#print out totals for destinations
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";
}

store { %total_bytes }, $output_file;




Report this thread to moderator Post Follow-up to this message
Old Post
Chris
08-24-07 09:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:55 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.