#!/usr/bin/perl -w require 5.006; # i'm using "open my $fh ..." construct use strict; # ================================================= # constants # put magic constants at top, so people can easily change them my $CHARGE_CODES_FILE = "mnta2/bmp/table/smsc/chg_indicator.hdb"; my $INPUT_TEMPLATE = "A12 A4 A20 A20 A2 A2 A2 A2 A12 A4 A24 A24"; my $SOURCE_IX = 2; my $DEST_IX = 3; # ================================================= # first, load up the charge database. my %charge_codes; { open my $fh, $CHARGE_CODE_FILE or die "$0: opening '$CHARGE_CODE_FILE': $!"; while ( <$fh> ) { next if /^#/; # skip comments s/\s+$/; # remove end of line chars # get fields from line, clean them up. my ( $src, $dest, $code ) = split /,/; $src =~ s/^0+//; $dest =~ s/^0+//; $charge_codes{$src}{$dest} = $code; } } # ------------------------------------------------- # process standard input, accumulate charges. my %charges_against; while ( my $input = <> ) { # extract source and destination values my @fields = unpack $INPUT_TEMPLATE, $input; my $src = $fields[ $SOURCE_IX ]; my $dest = $fields[ $DEST_IX ]; # remove leading zeros $src =~ s/^0+//; $dest =~ s/^0+//; # see if there are any charge codes associated with this pair. my $code = $charge_codes{$src}{$dest}; if ( defined $code ) { ++$charges_against{$code}; } } # ------------------------------------------------- # output the results foreach my $code ( sort keys %charges_against ) { my $count = $charges_against{$code}; print "Total calls for Billing Code [$code] : $count \n"; } # note that skip_zero is no longer necessary.