All pastes #50161 Raw Edit

dfsdfsd

public text v1 · immutable
#50161 ·published 2006-04-20 15:04 UTC
rendered paste body
#!/usr/bin/perl
use strict;
use warnings;

# The source host is the key for the hash %total_bytes.
# The value is a reference to an anonymous hash.
#
# In this anonymous hash the key is the destination host.
# The value is the amount of bytes totally sent to that destination by $source.

my %total_bytes;

while (<>) {
  if (/#.*/) {
    next;
  } else {
    my ($source, $destination, $bytes) = split;
    $total_bytes{$source}{$destination} += $bytes;
  }
}

# This prints the $value for gilligan.crew.hut, some amount of $bytes
#print "$total_bytes{'professor.hut'}->{'gilligan.crew.hut'}\n";
# This prints the HASH address
#print "$total_bytes{'professor.hut'}\n";
#print "$total_bytes{'gilligan.crew.hut'}\n";

for my $source (keys %total_bytes) {
  for my $destination (sort keys %{$total_bytes{$source} }) {
    $total_bytes{$source}{'total'} += $total_bytes{$source}{$destination};
  }
}

sub by_traffic {
  $total_bytes{$b}{'total'} <=> $total_bytes{$a}{'total'}
}

sub by_host {
  my $source = shift;
  $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a}
}

for my $source (sort by_traffic keys %total_bytes) {
  print "$source: \n";
   for my $destination (sort {by_host($source)}  keys %{$total_bytes{$source} }) {
     print "\t$destination: $total_bytes{$source}{$destination}\n";
   }
}