Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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;

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 (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
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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-01-23 19:16 GMT
Find Nodes?
    Voting Booth?