Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

anonymous hash slice

by tstock (Curate)
on May 18, 2004 at 17:31 UTC ( #354362=perlquestion: print w/ replies, xml ) Need Help??
tstock has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to initialize an anonymous hash with the slice syntax ? How would I edit the code below to skip using the %hash variable ? This is not important code wise, but not knowing how to do it is killing me.
my %hash; @hash{ @labels } = @values; $data->{keys_and_values} = \%hash;
Tiago

Comment on anonymous hash slice
Download Code
Re: anonymous hash slice
by eserte (Deacon) on May 18, 2004 at 17:35 UTC
    Do you mean something like
    @{ $data->{keys_and_values} }{ @labels } = @values;
    ?
Re: anonymous hash slice
by artist (Parson) on May 18, 2004 at 17:42 UTC
    $data->{keys_and_values} = { map { $labels[$_] => $values[$_] } 0..$#l +abels }
Re: anonymous hash slice
by geekgrrl (Pilgrim) on May 18, 2004 at 19:04 UTC
    however, if you use strict;
    you must declare
    my %hash;

    does anyone know otherwise?
Re: anonymous hash slice
by BrowserUk (Pope) on May 18, 2004 at 20:29 UTC
    $data->{'keys&values'} = do{ $_={}; @{$_}{ @keys } = @values; $_ };

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-07-31 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls