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!!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
}
foreach my $one_key (sort keys %FileAndState)
{
print "$one_key\n";
}
my %FileAndState = (
'al_lib1.txt' => 'AL',
'ca_lib1.txt' => 'CA',
...
);