Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: setting hash keys by array

by Aristotle (Chancellor)
on Sep 09, 2002 at 19:22 UTC ( #196404=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196404]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-01-24 03:21 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.