in reply to Simplifying a nest of if statements
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', ... );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Simplifying a nest of if statements
by bh_perl (Monk) on Jul 29, 2003 at 12:48 UTC | |
by Anonymous Monk on Jul 29, 2003 at 13:09 UTC | |
|
Re: Re: Subroutine
by bh_perl (Monk) on Jul 29, 2003 at 11:30 UTC |