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

Initilizing a list array is very tidy, but is there a better way to initilize a hash array?
my @data = ('valueA','valueB','valueC','valueD'); # or my @data = qw(valueA valueB valueC valueD); my %data; $data{'keyA'} = 'valueA'; $data{'keyB'} = 'valueB'; $data{'keyC'} = 'valueC'; $data{'keyD'} = 'valueD';

Replies are listed 'Best First'.
Re: Initilize a hash array
by pfaut (Priest) on Jan 04, 2003 at 23:34 UTC
    my %data = ( keyA => 'valueA', keyB => 'valueB', keyC => 'valueC', keyD => 'valueD' );
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Initilize a hash array
by fruiture (Curate) on Jan 04, 2003 at 23:36 UTC
    my %data = ( 'keyA' => 'valueA', keyB => 'valueB', # fancy => allows bareword 'keyC' , 'valueC', # equivalent );

    See perlop and perldata. And be carefull with terminology: better say only 'array' (the @ variable), 'hash' (the % variable) and 'list' (no variable), because a list is something quite different from an array and a hash is not too much an array.

    --
    http://fruiture.de
Re: Initialize a hash array
by behroozi (Beadle) on Jan 05, 2003 at 05:51 UTC
    Or even:
    my %data; @data{qw(keyA keyB keyC keyD )} = qw(valueA valueB valueC valueD);

    If you prefer a vertical alignment.
Re: Initilize a hash array
by Beatnik (Parson) on Jan 04, 2003 at 23:36 UTC
    my %data = qw( keyA valueA keyB valueB keyC valueC keyD valueD );
    or just
    my %data = (keyA=>"valueA",keyB=>"valueB",keyC=>"valueC",keyD=>"valueD +");
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.