Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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

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?