On 8/27/07, Mihir Kamdar <[EMAIL PROTECTED]> wrote:
>
>
>
> I can't recommend reinventing the wheel, I would use the module to
> > lookup the country name and the use hash where the country name is the
> > key and the rate is the value to get the value, but if you are dead
> > set on not using the module your best bet is something like this:
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > my %prefix_to_rate = (
> > 12 => 0.30,
> > 1234 => 0.35,
> > 134 => 0.50,
> > 44 => 0.70
> > );
> >
> > #if there are more than about 50 country codes
> > #get rid of the sort and implement min and max
> > #in Perl, otherwise sort is actually faster
> > my ($shortest, $longest) =
> > (sort { $a <=> $b } map {length} keys %prefix_to_rate)[0, -1];
> >
> > while (<DATA>) {
> > my ($num, $min) = split;
> > my $rate;
> > for my $len (reverse $shortest .. $longest) {
> > my $key = substr $num, 1, $len;
> > last if $rate = $prefix_to_rate{$key};
> > }
> > if (defined $rate) {
> > print "the rate for $num is $rate: ", $rate * $min,
> > "\n";
> > } else {
> > print "$num does not have a rate\n";
> > }
> > }
> >
> > __DATA__
> > +12555 10
> > +44555 20
> > +12345 30
> > +55555 55
> >
>
> Hi,
Please ignore the previous reply of mine. I have made changes and now the
code is not giving any errors, but is not giving me the desired result. Plz
check:-
#!/usr/bin/perl
use strict;
use warnings;
my $file_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/ratetest';
my $write_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/rateop'
;
my %prefix_to_rate = (
'+12' =>0.30,
'+1234' =>0.35,
'+134' =>0.50,
'+44' =>0.70
);
my %times ;
my ($shortest, $longest) =
(sort { $a <=> $b } map {length} keys %prefix_to_rate)[0,
-1];
my $continue = 1;
$SIG{INT} = $SIG{TERM} = sub { $continue = 0 };
while ($continue) {
opendir my $dh, $file_path or die $!;
while (my $file = readdir $dh) {
my $fname = "$file_path/$file" ;
next unless -f $fname;
unless (exists $times{$file}){
my $line;
open (my $IN_FILE,"<","$file_path/$file") or die
$!." file not found" ;
while ($line=readline($IN_FILE))
{
my @cdr=split (/,/, $line) ;
#($cdr[3],my $min) = split;
my $rate;
for my $len (reverse $shortest .. $longest) {
my $key = substr $cdr[3], 1, $len;
last if $rate = $prefix_to_rate{$key};
}
open (my $OUT_FILE,">","$write_path/$file.out") or die $!;
if (defined $rate) {
print $OUT_FILE "the rate for $cdr[3] is $rate: ",
($cdr[6]/20)*$rate , "\n";
}
else
{
print $OUT_FILE "$cdr[3] does not have a rate\n";
}
}
close $IN_FILE ;
}
}
closedir $dh ;
}
Thanks,
Mihir