Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

anonymous hash slice

by tstock (Curate)
on May 18, 2004 at 17:31 UTC ( [id://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

Replies are listed 'Best First'.
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 BrowserUk (Patriarch) 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
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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found