in reply to Re: Re: setting hash keys by array
in thread setting hash keys by array

They do this instead: $hash{red}{green}{brown} = { }
True, that's what comes out of the for loop. Then the next instruction in my sample turns it into the requested result.. Is the following more to your liking?
my $ptr = \%hash; $ptr = $ptr->{$_} = {} for @array[0..$#array-1]; # fixed; s/shift @arr +ay/$_/ $ptr->{$array[-1]} = 1;
or maybe
my $ptr = \%hash; $ptr = $ptr->{shift @array} = {} while @array > 1; $ptr->{$array[0]} = 1;
Update: forgot to remove the shift when I initially copypasted. See comment.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^3: setting hash keys by array
by blssu (Pilgrim) on Sep 09, 2002 at 21:03 UTC

    Did your $ptr = 1 line really work on your machine? It didn't on mine.

    At least your new code is correct... ;) BTW, if you use the -1 index on array, you don't need to shift @array in the loop -- that's the version I'd prefer in production code.

    Anyways, it was just fun looking at alternatives. It's not often I see a clean use of type globs!

      Oops. I wrote the while version first and forgot to change the shift into a plain $_ when I copypasted. Fixed.

      Makeshifts last the longest.