in reply to Hash slice again

Maybe you still haven't explained the problem correctly. When I undef a hash slice, only one of the values becomes undefined, and when I undef an array slice, only the last element becomes undefined.
use Data::Dumper; my @a = qw/a b c d e/; my %a; @a{@a} = (1) x @a; print "--- \@a before ---\n", Dumper \@a; print "--- \%a before ---\n", Dumper \%a; undef @a{@a}; undef @a[1,2,3]; print "--- \@a after ---\n", Dumper \@a; print "--- \%a after ---\n", Dumper \%a;
Yields this output:
--- @a before --- $VAR1 = [ 'a', 'b', 'c', 'd', 'e' ]; --- %a before --- $VAR1 = { 'e' => 1, 'c' => 1, 'a' => 1, 'b' => 1, 'd' => 1 }; --- @a after --- $VAR1 = [ 'a', 'b', 'c', undef, 'e' ]; --- %a after --- $VAR1 = { 'e' => undef, 'c' => 1, 'a' => 1, 'b' => 1, 'd' => 1 };

Replies are listed 'Best First'.
Re^2: Hash slice again
by targetsmart (Curate) on Jan 27, 2009 at 14:57 UTC
    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.

      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.