In Perl 5.10, how do I create and access a hash with scalar keys whose values are arrays?
#Doing this does not work properly.
%someHash= ("0xfff" => ('Blue', 'Red', 'Yellow'));
@arr = @fileContents{"0xfff"};
print @arr;
When I print the array, the only thing that prints is “ARRAY(‘randmemAddr’)”. When I do a foreach loop on @arr, only the first element is printed. I have then concluded that I have not stored the array properly in the hash.
My original answer posted working code, but didn’t really explain the problem with yours. This is expanded a bit to correct that. Your example had two problems. First, you had a problem when making the reference. You need to use
[ ]instead of the standard parentheses in order to create a reference (to an anonymous array). Second, when you tried to get at the reference, you left off one set of brackets. You want to put the reference itself inside@{ }in order to get at the whole array. (Also, and this may be a typo: you have no$beforefilecontents.)The code here is essentially from
perldoc perldsc. I highly recommend it. Also very useful if you’re new to references in Perl isperldoc perlreftut. Both tutorials discuss how to make and get at references in a variety of situations. Finally, you can find a good cheat sheet for references in a post on PerlMonks.