in reply to Re: Hash slice again
in thread Hash slice again

use strict; use warnings; use Data::Dumper; my @a = qw/a b c d/; my @b = qw/a b c d/; my %a; print '--- my @a before ---',"\n", Dumper \@a; undef @a[1,2]; print '--- my @a after ---',"\n", Dumper \@a; print '--- my %a before ---',"\n", Dumper \%a; undef @a{@b}; print '--- my %a after ---',"\n", Dumper \%a;
the above code prints
--- my @a before --- $VAR1 = [ 'a', 'b', 'c', 'd' ]; --- my @a after --- $VAR1 = [ 'a', 'b', undef, 'd' ]; --- my %a before --- $VAR1 = {}; --- my %a after --- $VAR1 = { 'c' => undef, 'a' => undef, 'b' => undef, 'd' => undef };

When I undef a hash slice, all the values becomes undefined, and when I undef an array slice, only the last element becomes undefined.
Why is it so is the question?.
UPDATE
The question can be asked like this, why the undef behaves differently when it is a new array/hash, compared to when it sees an existing array/hash
if @a = (1,2,3), then undef @a[1,2] is proper, undefines $a[2] just undef @b[2], is giving me @b's 0 ,1 and 2 indexes to be undef. if %hash = qw(a b c d) then undef @hash{qw(a c)} is proper, undefines + only $hash{c} just undef @hash1{qw(a c}} is giving me the values of the keys a and c + to be undef

-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Replies are listed 'Best First'.
Re^3: Hash slice again
by jethro (Monsignor) on Jan 27, 2009 at 15:23 UTC

    You are creating hash elements with undef @a{@b}. This is called autovivication (see wikipedia for a nice explanation) and has nothing to do with the undef function. Something similar happens when you write @a{@b}= (1);. The hash would contain key a set to 1 and keys b and c set to undef

      I have read about Autovivification from http://en.wikipedia.org/wiki/Autovivification, actually I previously read this from "Intermediate perl" page number 44, which discusses about autovivification, but I just have not applied in this case. Thanks for pointing out
      Explanation about autovivification,
      Any nonexistent variable, or a variable containing undef, which we dereference while looking for a variable location(technically called an lvalue context), is automatically stuffed with appropriate reference to an empty item, and perl allows the operation to proceed. -- from "Intermediate Perl" by Randal.L.Schwartz, brian d foy, Tom phoenix.
      this is also interesting http://www.sysarch.com/Perl/autoviv.txt

      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

        Any nonexistent variable, or a variable containing undef, which we dereference while looking for a variable location(technically called an lvalue context), is automatically stuffed with appropriate reference to an empty item

        That refers to

        $ perl -le'print $r||"[undef]"; $r->{x}; print $r||"[undef]";' [undef] HASH(0x814ec28)

        jethro was refering to a different kind of autovivification. Creating an alias (as undef does) to a non-existent array or hash element can create (vivify) the element in question (with value undef).

        $ perl -le'sub f {} print 0+keys %h; for ($h{x}) {} print 0+keys %h;' 0 1