in reply to Assign (key, value) to a hash w/o clobbering hash

Why not using map? This is concise and uses no temporary variable :
%hash = map { split( /X/, $_ ) } ( "fooXbar","bishXbash") ; # look Ma, that works! foreach(keys %hash){ print "1: $_ = $hash{$_}\n"; }

Replies are listed 'Best First'.
Re^2: Assign (key, value) to a hash w/o clobbering hash
by reasonablekeith (Deacon) on Sep 28, 2006 at 13:30 UTC
    because the OP doesn't want to clobber what's already in the hash. Assigning a list to a hash clears out the hash first, and then starts assigning the new values.

    Update: argh, see mreece's reply to this post

    Regardless, the map is completely redundant here, the following two are equivalent.

    %hash = map { split( /X/, $_ ) } ( "fooXbar","bishXbash"); %hash = split /X/, ("fooXbar","bishXbash");
    ---
    my name's not Keith, and I'm not reasonable.
      those are not at all equivalent!
      DB<1> x %hash = map { split( /X/, $_ ) } ( "fooXbar","bishXbash"); 0 'foo' 1 'bar' 2 'bish' 3 'bash' DB<2> x %hash = split /X/, ("fooXbar","bishXbash"); 0 'bish' 1 'bash'
      because the OP doesn't want to clobber what's already in the hash.

      Maybe, but the OP proposed a solution that do reset the hash :

      # Works, but needs redundent array undef %hash; @array = (split /X/, "fooXbar"); $hash{$array[0]} = $array[1]; @array = (split /X/, "bishXbash"); $hash{$array[0]} = $array[1]; foreach(keys %hash){ print "2: $_ = $hash{$_}\n"; }

      so what is to be achieved isn't that clear after all.