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', ... );

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

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
    Hi browserUK,

    Nothing to do, that my simple mistake and I'm had changed that.

    But, I've one question here. How I can moved the next variable into next line and passed to the sub routine, as example ?

    logitme ($rec, $calltype, $num1, $num2, $anum, $bnum, $cnum, $dur +, $intrunk, $freecall, $sms, $operatorTdb, $routehdb, $voicemail, $op +eratorGrp );
    That is very long variable, I have tried like this :

    logitme($rec, $calltype, $num1, $num2, / $anum, $bnum, $cnum, $dur, $intrunk, / $freecall, $sms, $operatorTdb, / $routehdb, $voicemail, $operatorGrp);

    But it's failed and come out with some error (as my previous mail). could you help me ?

    Thanks,
    bh_perl

      Just drop the slashes. I guess you meant backslashes, but they are not needed here.

Re: Re: Subroutine
by bh_perl (Monk) on Jul 29, 2003 at 11:30 UTC
    Hi BrowserUK,

    Thanks for your good advise,
    Once I'm followed your nice ideas, some error found here which are :
    Not a HASH reference at test.pl line ?? .....
    What does means ?, or I'm missing something like use strict or ???

    The error line refer to the "freecall" hash

    Hopefully, my text format is OK right now ;-)