tags:

views:

51

answers:

2
  my %geo_location_map = (
                             US => [ 'US', 'CA' ],
                             EU => [ 'GB', 'ES' ],

                           );
   $location= "US" ;
   my $goahead = 0;

    if (exists  $geo_location_map{US} ) {
    print "exists";
        my @glocation =  $geo_location_map{US};

    foreach @glocation { 
        if ( $_ eq "$location"} { $goahead=1; last;}  
        }
    }

I tried its not working

+4  A: 

$geo_location_map{US} contains an array reference; if you want to copy the array to @glocation you need to dereference it:

my @glocation = @{$geo_location_map{US}};
Jim Davis
+1  A: 

First of all, always "use strict" in your scripts. You had multiple errors. see :


my %geo_location_map = (
    US => [ 'US', 'CA' ],
    EU => [ 'GB', 'ES' ],
);
my $location= "US" ;
my $goahead = 0;

if (exists  $geo_location_map{US} ) {
    print "exists";
    my @glocation =  $geo_location_map{US};

    foreach (@glocation) {

        if ( $_->[0] eq "$location") {
            print "ahead\n";
                        $goahead=1;
            last;
        }
    }
}



As jim davis said, you had ann array ref. Moreover, some bracket errors, no big deal

benzebuth