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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.