0

Possible Duplicate:
How do I create a hash of hashes in Perl?

I need to create something which is an equivalent of map : name(string) to map date(string/int) to value, i.e. a map { string => map { string => value } } . How should i go about it in perl ? The following code does not work.

my %strtomap_;
# given $str_, $date_, $val_
if ( ! exists $strtomap_ { $str_ } )
{
 my %new_map_date_to_val_ ;
 $new_map_date_to_val_{$date_} = $val_;
 $strtomap_ { $str_ } = %new_map_date_to_val_ ;
}
else
{
 $strtomap_ { $str_ } { $date_ } = $val_;
}
Community
  • 1
  • 1
Humble Debugger
  • 4,439
  • 11
  • 39
  • 56
  • 2
    There's value in producing an answer under this title as other people who don't know Perl may use the incorrect term "map" to describe Perl's associative arrays (i.e. hashes). – Conspicuous Compiler Aug 28 '11 at 22:52
  • 2
    @Conspicuous, unfortunately since `map` does have a meaning in Perl this is causes more confusion than its worth. – Joel Berger Aug 29 '11 at 02:49
  • @Joel: There's no doubt it's confusing if somebody continues to use the wrong terminology, but map is the term used in Python as well as other languages. A Python programmer who hasn't done any research looking for help with hashes will search for "a map of maps in Perl". Best to provide help to people in the terms they know while teaching them the new terminology, or they may never find what they need. – Conspicuous Compiler Aug 29 '11 at 13:43
  • @Conspicuous, while I understand that alternate terminology happens, there are many different terms, ought we to have a thread for each variant? I believe that a decent programmer would investigate the terminology of the language being used and search correctly. – Joel Berger Aug 29 '11 at 15:19
  • 1
    @Joel 'ought we to have a thread for each variant?' Precisely, yes. – Conspicuous Compiler Aug 29 '11 at 15:32
  • @Conspicuous, I guess we disagree then, I expect someone coming for help to have done at least SOME due diligence – Joel Berger Aug 29 '11 at 19:45
  • 1
    but part of the goal here is to assemble a FAQ that can be one of the first steps of due diligence. don't begrudge the server resources to have a single FAQ targeted to those who may not even realize they don't know their target language's proper terminology. – ysth Aug 29 '11 at 21:40

1 Answers1

5

Values of aggregate types (hashes and arrays) can only be scalars, not other aggregate types. So if you are going to explicitly use something like your %new_map_date_to_val_, you need to store a reference to it instead:

my %new_map_date_to_val;
$new_map_date_to_val_ { $date_ } = $val_;
$strtomap_ { $str_ } = \%new_map_date_to_val_;

though you can use an anonymous hash instead:

$strtomap_ { $str_ } = { $date_ => $val _ };

And in fact, the whole exists test is not needed, since simply dereferencing an undefined value in this kind of way will autovivify a hash reference for you. Your whole code can just be:

my %strtomap_;
$strtomap_ { $str_ } { $date_ } = $val_;
ysth
  • 96,171
  • 6
  • 121
  • 214