Link to home
Start Free TrialLog in
Avatar of mkido
mkido

asked on

HASH, a beginner needs an introduction, how to address array-elements?

Hello!  Everyone,

Here is my brand new simple HASH, associated array.

   % FileAndState = qw (
   
     al_lib1.txt    AL
     ca_lib1.txt   CA
     fl_lib1.txt     FL
     ga_lib1.txt    GA

          );

I can print the State out by,

   print $FileAndState{"al_lib1.txt"};
   print $FileAndState{"ca_lib1.txt"};
   print $FileAndState{"fl_lib1.txt"};
   print $FileAndState{"ga_lib1.txt"};

This prints " AL  CA  FL  GA".     Now, how to address (or print) the keys (in this case, Filenames, such as al_lib1.txt, ca_lib1.txt, fl_lib1.txt, and ga_lib1.txt ...) as themselves.   Should be fundamental knowledge, but I don't know yet.   Thank you for your help!!

SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also it is a bit easier toread if you declare your hashes like this:

my %FileAndState =  (

     'al_lib1.txt' => 'AL',
     'ca_lib1.txt' => 'CA',
     ...
);
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mkido
mkido

ASKER

Thank you, teraplane, I found "sort" is necessary to print out in order.

foreach my $one_key (sort keys %FileAndState)
{
   print "$one_key\n";
}