To add to Fletch answer I have ran your code and noticed an error message that array @keys is not declared and you can do this with something as "my @keys;". You could delete the last while loop and save the keys in array @keys. And then cycle through the array like this: