in reply to Re^2: Possible for Map to create Hash of Hash?
in thread Possible for Map to create Hash of Hash?

untested
my %hash = map { my @explicit = split //, $_; $explicit[0] => { $explicit[1] => 1 }; } <>;

Replies are listed 'Best First'.
Re^4: Possible for Map to create Hash of Hash?
by konnjuta (Acolyte) on May 11, 2013 at 11:33 UTC
    I don't believe this would work. Firstly,
    split //, $_
    returns a list of characters instead of a list of words. Secondly,
    $explicit[0] => { $explicit[1] => 1 };
    Will ensure the store(i.e the key) will only have the last product read from the input. Update: or As Rolf puts it succinctly key Collision of the top level hash.

      I don't believe this would work. Firstly,

      Doesn't matter, poulate @explicit however you like

      Secondly, Will ensure the store(i.e the key) will only have the last product read from the input.

      Is this important?

      This is how you're supposed to use map, to create lists -- if you're not doing that stick with foreach (or to save memory stick with while)

      If all you want is to treat it like foreach, you can use it in void context, but its not recommended, it clouds the purpose, and on older perls its not efficient

      #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd pp /; open my($fh),'<', \<<'__FUDGE__' or die; STORE PRODUCT_ID ==================== Sydney 12 Sydney 14 Canberra 12 Canberra 18 __FUDGE__ my %hash; $hash{(split)[0]}{(split)[1]} = 1 while <$fh>; seek $fh, 0, 0; dd\%hash; undef %hash; map { my @explicit = split ' ', $_; 2 == @explicit ## ==== and $hash{ $explicit[0] }{ $explicit[1] }++; (); } <$fh>; dd\%hash; __END__ $ perl fudge Use of uninitialized value in hash element at fudge line 16, <$fh> lin +e 6. { "====================" => { "" => 1 }, "Canberra" => { 12 => 1, 18 => 1 }, "STORE" => { PRODUCT_ID => 1 }, "Sydney" => { 12 => 1, 14 => 1 }, } { Canberra => { 12 => 1, 18 => 1 }, STORE => { PRODUCT_ID => 1 }, Sydney => { 12 => 1, 14 => 1 }, }
        Is this important?
        Very important!