Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: setting hash keys by array

by Shendal (Hermit)
on Sep 09, 2002 at 16:13 UTC ( #196328=note: print w/ replies, xml ) Need Help??


in reply to setting hash keys by array

How about something like this?

#!/usr/bin/perl -w use strict; use Data::Dumper; my @ary = qw(red green brown); my %hash = (); eval '$hash{' . join('}{',@ary) . '}++;'; print Dumper(\%hash);

That prints:
$VAR1 = { 'red' => { 'green' => { 'brown' => '1' } } };

Cheers,
Shendal


Comment on Re: setting hash keys by array
Select or Download Code
Re: Re: setting hash keys by array
by suaveant (Parson) on Sep 09, 2002 at 18:04 UTC
    This is probably not a great way to do it... not only is it inefficient and open to syntax errors (i.e. a key of ';}' would be bad), but if this was something that used user input it could be a security hole... ( @ary = '},`rm -rf /`,{' )

    eval is usually best used as a last resort and for exception handling (though this is usually with existing written code)

                    - Ant
                    - Some of my best work - (1 2 3)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-07-30 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls