Question by Licursi: How do you print the values of a hash in PERL by using a key?
Can someone give me an example please. I have a hash table indexed by a key and I want to print it out but I need the syntax of how to do so.
Thanks
Best answer:
Answer by Bobby
#First declare a hash
my %my_hash;
#Fill it with some stuff
$my_hash{‘key 1′} = ‘Some Data’;
$my_hash{100} = ‘More Data’;
#Now print out some values
print “Here is a single value $my_hash{100}\n”
print “Here is another $my_hash{‘key 1′}\n”
#Now print out all values/keys
foreach my $key (keys(%my_hash)) {
print “The key is $key the value is $my_hash{$key}}\n”;
}
#Important things to know about PERL. prefixing a $ to a variable means treat the data like a scalar value. A prefix of % means treat the data like a hash and a @ means treat it like an array.
Give your answer to this question below!