On 8/27/07, Mihir Kamdar <[EMAIL PROTECTED]> wrote:
snip
> But i have a list of 65 countries, and I only want to implement for them.
> Also, as you said, definitely for some, prefix length is only 1 digit, while
> for others, it is 2 or 3 digits. So, I was thinking to build a hash and then
> implement this.
> Also, my aim is not actually to get the country name.
> On the basis of the prefix, I have the corresponding callrate/sec. Foe ex,
>
> Country_Name       Country_Code   Rate/sec
>     Argentina 54 RM0.99
> So, if the longest prefix match of the 6th field(called_number) of my CSV
> file matches with country code of Argentina, then read the 13th
> field(Duration), divide it by 60 and multiply it by the rate/sec. of
> Argentina and write the result into the 14th field.
>
> please advice on how something like this can be achieved??
>
> Thanks,
> Mihir

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

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


Reply via email to