Last active
June 7, 2016 02:23
-
-
Save ishu3101/4d76c233b60a8e83726622e4ab670b85 to your computer and use it in GitHub Desktop.
Dictionary (also called Hashes, Associative Array or Maps) with Single Key and Multiple Values Example in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%letters = ( | |
"a" => ["Apple", "Aeroplane"], | |
"b" => ["Bat", "Banana", "Basket"], | |
"c" => ["Cat", "Carpet"], | |
); | |
# loop through just the keys | |
for my $key (sort keys %letters){ | |
$value = $letters{$key}; | |
$length = @{$value}; | |
print "Key: $key \n"; | |
print "Value: @{$value} \n"; | |
print "Length: $length \n"; | |
} | |
print "\n"; | |
# loop through the both the keys and the values | |
for my $key (sort keys %letters){ | |
$value = $letters{$key}; | |
@length = @{$value}; | |
for my $val (@length){ | |
print "Key: $key, Value: $val \n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Key: a | |
Value: Apple Aeroplane | |
Length: 2 | |
Key: b | |
Value: Bat Banana Basket | |
Length: 3 | |
Key: c | |
Value: Cat Carpet | |
Length: 2 | |
Key: a, Value: Apple | |
Key: a, Value: Aeroplane | |
Key: b, Value: Bat | |
Key: b, Value: Banana | |
Key: b, Value: Basket | |
Key: c, Value: Cat | |
Key: c, Value: Carpet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment