Rather than all those individually named totals and counts, use a hash to store them and a sub to make totalling them generic. Just add more osnames to the initialisation of the hash, and you never need change the rest of the program when a new one comes along.
sub logtime { my( $oref, $freecall, $voicemail, $osname, $dur, $bnum ) = @_; $oref->{ totaldur }{ $osname } += $dur; $oref->{ totalcdr }{ $osname }++; if( defined $freecall->{ $bnum } ) { $oref->{ freedur }{ $osname } += $dur; $oref->{ freecdr }{ $osname }++; } elsif( defined $voicemail->{ $bnum } ) { $oref->{ voicedur }{ $osname } += $dur; $oref->{ voicedcr }{ $osname }++; } } my( %o, %freecall, %voicemail, $osname, $dur, $bnum ); # Init with all known osnames. @o{ qw[ INTIM TOUCH ... ] } = (); if ( defined $osname{$num1} and exists $o{ $osname{ $num1 } } ) { logtime( \%o, \%freecall, \%voicemail , $osname{ $num1 }, $dur, $bnum ); } else { warn 'Undefined or unknown osname'; }
Note: This isn't meant to be working code, just a starting point. You could/should? probably combine the freecall and voicemail hashes into one.
my %calls = ( voice => { 123 => undef, 789 => undef, ], free => { 456 => undef, ], );
If they are mutually exclusive, you could key the combined hash the other way
my %calls = ( ## Keys are possible $bnum values 123 => 'voice', 456 => 'free', 789 => 'voice', ... );
In reply to Re: Subroutine
by BrowserUk
in thread Simplifying a nest of if statements
by bh_perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |