in reply to Re: from array to hash
in thread from array to hash

Thanks, but what if the hash already has some values, like:
#! /usr/bin/perl @a = qw(a b c d ); $b{x} = 10 ; %b = map { $_, 1} splice(@a, 0, scalar(@a) ); print "$_ --> $b{$_}\n" foreach (keys %b) ;
all values of %b are erased using map.....
Furthermore, is scalar(@a) really necessary, or can you as well do @a ?

Luca

Replies are listed 'Best First'.
Re^3: from array to hash
by friedo (Prior) on Mar 09, 2006 at 15:48 UTC
    In that case, you can use a loop instead:

    $b{$_} = 1 for grep defined, @a;
Re^3: from array to hash
by ikegami (Patriarch) on Mar 09, 2006 at 17:20 UTC

    You've severly misunderstood the use of splice. Did you read the docs?

    @a = qw(a b c d ); undef $a[2]; $b{x} = 10 ; $b{$_} = 1 foreach grep defined, @a;

    or

    @a = qw(a b c d ); splice(@a, 2, 1); $b{x} = 10 ; $b{$_} = 1 foreach @a;