Skip to content

Instantly share code, notes, and snippets.

@ishu3101
Last active June 7, 2016 02:23
Show Gist options
  • Save ishu3101/4d76c233b60a8e83726622e4ab670b85 to your computer and use it in GitHub Desktop.
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
%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";
}
}
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