hunt1218 has asked for the wisdom of the Perl Monks concerning the following question:

The person who wrote this code as left the company (many moons ago) and we in the process of converting to a new application but we are not able to understand the Perl script used to calculate abandoned calls. This is the specfic are of the script that would count these call types (I have a lot more code if that's needed). Anything you can do would be greatly appreciated.

sub get_abandonned($) { my $dnis = shift; my $total = 0; for my $key(keys %all_calls) { next if ($key =~ /duplicate/); if($all_calls{$key}->[DNIS] eq $dnis && $all_calls{$key}->[SPE +ECH] == 0 && $all_calls{$key}->[TRANSFER] eq "hungup") { my $time_length = &get_call_length($all_calls{$key}->[TIME +]); if($time_length <= -29) { $total++; } } } return $total; }

Replies are listed 'Best First'.
Re: Help with Perl Script
by davido (Cardinal) on Jan 07, 2015 at 20:27 UTC

    You didn't say what part of it your team is having difficulty understanding, so here's a basic rundown of what is going on:

    sub get_abandonned($) { # Subroutine de +finition. my $dnis = shift; # $dnis will ho +ld # the parameter +. my $total = 0; # $total holds +0. for my $key(keys %all_calls) { # There is a ha +sh named # %all_calls. +Iterate # over the keys + of the # hash and assi +gn each # key to $key o +n its # respective it +eration. next if ($key =~ /duplicate/); # Proceed to ne +xt # iteration if +the # current key c +ontains # the text 'dup +licate'. if( # If all of the # following are + true: $all_calls{$key}->[DNIS] eq $dnis && $all_calls{$key}->[SPEECH] == 0 && $all_calls{$key}->[TRANSFER] eq "hungup" ) { # then do the f +ollowing: my $time_length # Give a value +to # time_length, = &get_call_length($all_calls{$key}->[TIME]); # from this fun +ction # call. if($time_length <= -29) { # Now if this i +s true... $total++; # increment $to +tal. } } } # Move on to ne +xt key. return $total; # return the $t +otal. }

    Dave

Re: Help with Perl Script
by LanX (Saint) on Jan 07, 2015 at 20:12 UTC
    plz help us reading your snippets and put it within <code>  ...  </code> tags

    update

    I tried to decipher your code and there is nothing particular "perlish" that we could explain. A hash of (phone?)"calls" is traversed and filtered by different criterias.

    Maybe you should hire someone who understands your business logic and algorithms in general.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Help with Perl Script
by Anonymous Monk on Jan 07, 2015 at 20:41 UTC

    An approximate translation, note I'm inferring/guessing a few things (such as that %all_calls contains a set of call records):

    • The function get_abandonned takes one argument, I'm guessing this is some kind of identifier (DNIS = Dialed Number Identification Service?)
    • It loops over the set of call records in %all_calls, skipping those whose key contains the string "duplicate", and selecting those where the "DNIS" item in the record matches the one given to the function as an argument and whose "SPEECH" value is zero and whose "TRANSFER" value is "hungup"
    • For each of those records, it calls the function get_call_length, giving it the "TIME" item from the call record; if the value that the function returns is less than or equal to -29, increment the "total" counter by one.
    • After having processed all the records, the function returns the "total" counter.

    A note on the apparent data structure: %all_calls is a hash that contains a set of key/value pairs, where the keys sometimes contain the word "duplicate" (can't tell more than that from this code), and the values are arrays (arrayrefs to be more specific). I'm guessing that DNIS, SPEECH, etc. are integer constants are the indices of those arrays.

      This section of code is supposed to count abandoned calls. Your explanation makes complete sense. Thank you so much!