On 06/02/2012 21:14, sono...@fannullone.us wrote:
use strict;
use warnings;

my %states = (
        AL =>  [ '350','351', ],
        AK =>  [ '995','996', ],
        AZ =>  [ '850','851', ],
        AR =>  [ '716','717', ],
);

my $customers_state = 'AZ';
my $customers_zip = '850';
my $match = 'no' ;

STATE:  foreach my $state (keys %states) {
#                       print "$state \n";
                        if ($state eq $customers_state) {
                                foreach (@{$states{$customers_state}}) {
                                        my @zips = $_;
ZIP:                            foreach my $zip (@zips) {
                                                next ZIP if $zip ne 
$customers_zip;
                                                        $match = 'yes';
#                                                       print "\nZip matches the 
State \n";
                                        }
                                }
                        last STATE;
                        }
                }
print $match;

########################

You are missing the point of hashes: that they can be indexed directly by a key value, and there is no need to loop through all elements of the has. You can reduce you code to just one loop:

  foreach my $zip (@{$states{$customers_state}}) {
    if ($zip eq $customers_zip) {
      $match = 'yes';
      last;
    }
  }


HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to