in reply to hash and it's keys...

The problem in your code is,

you are assigning the number of keys in the hash to the variable $key.

Use,

foreach $key (keys %hash){ print $key."\n" }