in reply to Using map to interlace an array
my @keynames = qw/first second third fourth/; my %hash = map { $_, 0 } @keynames;
you can initialize a hash with values from an array as follows (this is called a hash slice):
my @keynames = qw/first second third fourth/; my @squares = map { $_ ** 2 } (1 .. 4); my %hash; @hash{@keynames} = @squares;
also, in your example, i don't think the hash is totally empty. it should have a single undefined element in $hash{0}. i believe this is because the map variant you were using was treating the final value from your list (i.e. '0') as the map code and overwriting that element in the hash with undef for every element in @keynames.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Using map to interlace an array
by athomason (Curate) on May 15, 2000 at 11:59 UTC |