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,
I tried to implement the way Chas suggested, but am getting a lot of errors.
Plz check my mistakes:-
#!/usr/bin/perl
use strict;
use warnings;
my $file_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/ratetest';
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};
}
if (defined $rate) {
print "the rate for $cdr[3] is $rate: ", $rate * $min, "\n";
}
else
{
print "$cdr[3] does not have a rate\n";
}
}
}
}
closedir $dh ;
}
Thanks,
Mihir