in reply to From: array To: complex hash

my @list=qw(a b c d e f g); my %hash; my $hptr=\%hash; $hptr = $hptr->{$_}={} foreach @list;
This will work. the innermost key will point to an empty hashref.

-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: Re: From: array To: complex hash
by demerphq (Chancellor) on Jul 07, 2003 at 17:40 UTC

    This will work. the innermost key will point to an empty hashref.

    Except it will clobber already existing keys/subhashes.

    my @list=qw(a b c d e f g); my %hash; my $hptr=\%hash; $hptr = $hptr->{$_}||={} foreach @list;

    Change that assignment to ||= and it works fine. :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Re: From: array To: complex hash
by markguy (Scribe) on Jul 07, 2003 at 17:19 UTC
    Thanks pete, that seems to be just what I'm looking for. In an attempt to further my own knowledge while unfortunately looking like a complete newb at this perl stuff, is there any way I can con you into explaining how you arrived at that particular solution?

    Thanks to everyone else's input... it's appreciated.
      I know that each element in the list is going to be a pointer to the one before it. I just save off a reference to where the next insertion should take place. Each time through I create a anonymous hash, insert it into the current hash (the key is the array element, the value is the anonymous hash) and store out a reference to the new hash for the next time through the loop.

      Hope that's clear. I'm much better at writing the code than explaining it in english.

      -pete
      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."