On 8/27/07, Jeff Pang <[EMAIL PROTECTED]> wrote: > > 2007/8/27, Mihir Kamdar <[EMAIL PROTECTED]>: > > The sample code below on executing gives result as:- > > Macau > > Hong Kong > > > > But there are 0 keys in the hash. I expected the result to be > > Hong Kong > > Macau > > > > Why is the hash getting empty here? > > > > > > #!/usr/bin/perl > > > > my %countries = ('+852' => 'Hong Kong', '+853' => 'Macau' ); > > > > my @string = {'+8521235567','+8531764458'} ; > > > > > > while (my($prefix,$country) = each %countries) > > > > { > > > > $prefix = substr($string,1,3); > > > > print "there are " . (keys %hash) . " keys in hash\n"; > > > > print $country."\n" ; > > > > } > > > > The codes have some problems.I'd modify them to, > > use strict; > use warnings; > > my %countries = ('+852' => 'Hong Kong', '+853' => 'Macau' ); > my @string = ('+8521235567','+8531764458'); > > for (@string) { > my $prefix = substr($_,0,4); > print $countries{$prefix},"\n"; > } > > __DATA__ > Hong Kong > Macau > > But as Chas and Martin said,you'd better use that CPAN module for doing > this. > Because countries's tel prefixes length are not the same. > thanks,
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