in reply to hash of lists - hash of hashes

hmm, preview gets rid of my brackets.... to repeat,
i what to change this:
%HoL = ( fruits     => [ "orange", "banana", "kiwi" ],
         vegetables => [ "carrot", "potato" ],
         cheeses    => [ "cheddar", "american" ]
       );
to this:
%HoH = ( fruits     => { orange => 1, banana => 1, kiwi => 1 },
         vegetables => { carrot => 1, potato => 1 },
         cheeses    => { cheddar => 1, american => 1}
        );

Replies are listed 'Best First'.
Turning Hash of Arrayrefs into Hash of Hashrefs
by merlyn (Sage) on Jun 13, 2000 at 04:33 UTC
    In place, you could do something like this:
    for (@HoL{keys %HoL}) { $_ = {map {$_, 1} @$_}; }
    (You can use values in place of the slice in 5.6, but I'm not there yet.)

    If you really need a copy, that's a little odder, like this:

    %HoH = map { $_ => {map { $_, 1 } @{$HoL{$_}}} } keys %HoL;
    Untested, but I think I got my indirections right.

    -- Randal L. Schwartz, Perl hacker