0
  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

1
  • Please bee more specific about what is "not working". What happens, vs. what do you want to have happen? Also, you must always use strict; use warnings; in your scripts, if you wish to have perl report errors to you. Commented Jul 29, 2010 at 16:14

2 Answers 2

4

$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}};
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.