[Za-pm] Graphs with Perl

Bartho Saaiman bartho at cae.co.za
Thu Jun 5 02:56:45 CDT 2003


Since I am on a go now, I decided to add some more..

I have attached the code to the latest 'version' of my script and some 
output. Any comments...

<output>
[bartho at poseidon tmp]$ perl bartho.pl < bartho

bartho at cae.co.za: 9,833 byte(s)
- 27 72 341-8626:             5,553 ****************************
- bartho:                     3,156 ****************
- 27 82 928-5537:               324 *
- 27 84 500-0674:               303 *
- 27 82 575-4054:               237 *
- 27 83 273-6289:                77
- bgmilne:                       50
- 27 84 575-4054:                43
- 27 82 472-2231:                39
- 27 82 390-6668:                37
- michaelc:                      14
</output>

-- 
# Bartho Saaiman
# Stellenbosch Automotive Engineering
# Tel :: 27 21 882 8820 x 215
# Cell :: 27 82 551 2703
# Email :: bartho @ cae.co.za
# GnuPG Key is available at http://www.cae.co.za/people/bartho

-------------- next part --------------
#!/usr/bin/perl
use strict;
#use diagnostics;

my %total;

sub commify
{
  my $text = reverse $_[0];
  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  
  return scalar reverse $text;
}

sub pad
{
  my ($text) = @_;
  my $padding = '               ';

  return substr($padding, 0, -length($text));
}

sub padphone
{
  my ($number) = @_;
  
  return "$1 $2 $3-$4"
    if ($number =~ /^(\d{2})(\d{2})(\d{3})(\d{4})$/);

  return $number;
}

sub graph
{
  my ($size, $total) = @_;
  my $bar = '**************************************************';

  return substr($bar, 0, int($size * length($bar) / $total));
}

while (<>)
{
  my ($from, $bytes, $to) = /^.*? SMS  .*?(\w+\@[.\w]+).*? sent (.*?) characters to (.*?)$/;

  if ($from and $bytes and $to)
  {
    $to = padphone($to);
  
    $total{$from}{total} += $bytes; # totals
    $total{$from}{to}{$to} += $bytes; # sub-totals
  }
  else
  {
    warn 'Ignoring: ' . $_ . "\n";
  }
}

print "\n";

foreach my $from (keys %total)
{
  my $total = $total{$from}{total};
  
  print $from . ': ' . commify($total) . ' byte(s)' . "\n";

  foreach my $to
  (
    sort
    {
      $total{$from}{to}{$b} <=> $total{$from}{to}{$a}
    }
    keys %{$total{$from}{to}}
  )
  {
    my $bytes = commify($total{$from}{to}{$to});
    
    print
      '- ' . $to . ': ' . pad($to) . ' ' . 
      pad($bytes) . $bytes . ' ' . 
      graph($total{$from}{to}{$to}, $total) . "\n";
  }
    
  print "\n";
}



More information about the Za-pm mailing list