On 8/27/07, Mihir Kamdar <[EMAIL PROTECTED]> wrote:
>
>
> On 8/27/07, Chas Owens <[EMAIL PROTECTED]> wrote:
> > On 8/27/07, Mihir Kamdar <[EMAIL PROTECTED]> wrote:
> > snip
> > >                                 for my $len (reverse
> $shortest .. $longest) {
> > >                                 my $key = substr
> $cdr[3],0,$len;
> > >                                 last if $rate =
> $prefix_to_rate{$key};
> > >                                         if (defined
> $rate) {
> > >                                         $cdr[13]
> =($cdr[6]/20)*$rate ;
> > >                                         }
> > >                                 }
> > snip
> >
> > The last will execute if $rate is set, so the "if (defined" will never
> > be true (unless rate is 0, which I would assume is an error).  You
> > either need to move the "if (defined" outside of the for loop or you
> > need to change the "last if" to
> >
> > if (exists $prefix_to_rate{$key}) {
> >     $cdr[13] = $cdr[6]/20*$prefix_to_rate{$key};
> >     last;
> > }
> >
> > Also, you need to work on your indenting.  It is all over the place
> > (which is probably why your if statement is in the wrong place).  You
> > might consider installing perltidy and running it on your code.
> >
> Hi Chas,
>
> I modified it like below:-
>
>            while ($line=readline($IN_FILE))
>             {
>                 my @cdr=split (/,/, $line) ;
>                 my $rate;
>                 if($cdr[22] eq "Budget-IDD" )
>                 {
>                     for my $len (reverse $shortest .. $longest)
>                     {
>                         my $key = substr $cdr[3],0,$len;
>                         last if $rate = $prefix_to_rate{$key};
>                     }
>                     if (defined $rate)
>                     {
>                         $cdr[13] = ($cdr[6]/20)*$rate ;
>                     }
>                 }
>             $line = join(",",@cdr);
>             [EMAIL PROTECTED],3,6,7]}=$line;
>             }
>             close $IN_FILE ;
>
>
> Still not getting the result?
>
> Thanks..
>

#!/usr/bin/perl

use strict;
use warnings;

Off hand I would say the problem is in your hash key.  The following
outputs data as I would expect.  Let me know where/if it differs from
what you want.

my %prefix_to_rate = (
    '+1'   => 0.20,
    '+65'  => 0.30,
    '+673' => 0.45,
    '+86'  => 0.45,
    '+852' => 0.45,
    '+853' => 0.45,
    '+66'  => 0.45,
    '+212' => 1.50,
    '+234' => 1.50,
    '+47'  => 1.50,
    '+48'  => 1.50,
    '+7'   => 1.50,
    '+27'  => 1.50,
    '+46'  => 1.50,
    '+380' => 1.50,
    '+93'  => 1.50,
    '+62'  => 0.22
);

my ( $shortest, $longest ) =
  ( sort { $a <=> $b } map { length } keys %prefix_to_rate )[ 0, -1 ];

my %hash;
while ( my $line = <> ) {
    my @cdr = split( /,/, $line );
    my $rate;
    if ( $cdr[22] eq "Budget-IDD" ) {
        for my $len ( reverse $shortest .. $longest ) {
            my $key = substr $cdr[3], 0, $len;
            last if $rate = $prefix_to_rate{$key};
        }
        if ( defined $rate ) {
            $cdr[13] = ( $cdr[6] / 20 ) * $rate;
        }
    }
    $line = join ',', @cdr;
    $hash{join ',', @cdr[2,3,6,7]} = $line;
}

print $hash{$_} for sort keys %hash;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to