Hashes
The keys function
The keys function yields a
list of all the current keys in a given hash.
Example:
#!/usr/local/bin/perl
use strict;
use warnings;
my %prices = ("shirt" => 45,
"pullover" => 90,
"trousers" => 120,
"socks" => 15);
my @items = keys (%prices);
print "ITEMS: @items\n";
Result:
ITEMS: pullover shirt socks trousers
Iterating over all hash elements using the keys function:
Example - printing all keys and values of the %prices hash:
my @items_list = keys (%prices);
foreach $item (@items_list) {
print "$item : $prices{$item} NIS\n";
}
or, shorter:
foreach $item (keys (%prices)) {
print "$item : $prices{$item} NIS\n";
}
Result:
pullover : 90 NIS
shirt : 45 NIS
socks : 15 NIS
trousers : 120 NIS
Next.