http://qs321.pair.com?node_id=354362

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?