3 # The program from Exercise 2 in Chapter 5 needs to read the entire data
4 # file each time it runs. However, the Professor has a new router logfile
5 # each day and doesn't want to keep all that data in one giant file that
6 # takes longer and longer to process.
8 # Fix up that program to keep the running totals in a data file so the
9 # Professor can run it on each day's logs to get the new totals. Use the
16 use Storable qw(nstore retrieve);
20 my $storagepath = "ex6-1.data";
21 if (-e $storagepath) {
22 %hosts = %{retrieve $storagepath};
26 my ($src, $dst, $bytes) = split;
27 $hosts{$src}{$dst} += $bytes;
29 nstore \%hosts, $storagepath;
33 foreach (keys %{$hashref}) {
34 $total += $hashref->{$_};
38 foreach my $src (sort { sum($hosts{$b}) <=> sum($hosts{$a}) } keys %hosts) {
39 print "Total bytes ($src): ". sum($hosts{$src}) ."\n";
40 foreach my $dst (sort { $hosts{$src}{$b} <=> $hosts{$src}{$a} }
41 keys %{$hosts{$src}}) {
42 print "$src => $dst $hosts{$src}{$dst}\n";