rajaguha has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I often find myself doing something like the following:
my $hash_ref; @{$hash_ref}{qw/a b c d/} = (qw/A B C D/); push @array_of_hash_refs, $hash_ref;
Now, is it possible to write the same thing w/o using the intervening $hash_ref?
i.e. I have two lists - the keys, and the values. I want to merge them into an anonymous hash without using a hash reference or hash declaration.

Replies are listed 'Best First'.
Re: Slicing hashes that don't exist
by Corion (Patriarch) on Sep 08, 2010 at 12:26 UTC
    use List::MoreUtils qw(zip); my @keys = qw( a b c d ); my @values = qw( A B C D ); push @array_of_hash_refs, {+zip @keys, @values};
Re: Slicing hashes that don't exist
by BrowserUk (Patriarch) on Sep 08, 2010 at 13:48 UTC

    Sure:

    my @AoH; ... @{ $AoH[ @AoH ] }{ qw[ a b c ] } = qw[ 1 2 3 ]; pp \@AoH; __END__ [{ a => 1, b => 2, c => 3 }]
Re: Slicing hashes that don't exist
by moritz (Cardinal) on Sep 08, 2010 at 12:52 UTC
    Or in Perl 6:
    push @array_of_hashes, { <a b c d> Z <A B C D> };
    Perl 6 - links to (nearly) everything that is Perl 6.