in reply to Hash of array.

Hi,

a quick example under the Perl debugger:

DB<1> %hash = ( jan => 1, feb => 2, mar => 3); DB<2> push @array, $hash{$_} for keys %hash; DB<3> x \@array; 0 ARRAY(0x20275150) 0 1 1 3 2 2

Replies are listed 'Best First'.
Re^2: Hash of array.
by hippo (Archbishop) on Feb 09, 2017 at 11:25 UTC
    push @array, $hash{$_} for keys %hash;

    That will work (as shown) but to me seems very unwieldy when we could use values instead:

    push @array, values %hash;

    Does using for and keys buy you anything here?

      You're right, hippo++. I used that construct for pedagogical purpose because I thought at the time that looping over the keys to pick up the values was showing in a better way what's going on, but, thinking again about it, it's true that using values or keys is just the the same idea after all, and that using values makes it possible to use a simpler syntax, be is in a for loop as my post above or, even better, in a direct array assignment.