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. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/