Hello milanpwc,
From the documentation exists:
A hash or array element can be true only if it's defined and defined o
+nly if it exists, but the reverse doesn't necessarily hold true.
So why you are expecting to be true on not defined elements (not existing elements)?
Is it more clear or still confusing?
Also take notice from documentation:
exists may also be called on array elements, but its behavior is much
+less obvious and is strongly tied to the use of delete on arrays.
WARNING: Calling exists on array values is strongly discouraged. The n
+otion of deleting or checking the existence of Perl array elements is
+ not conceptually coherent, and can lead to surprising behavior.
Update: I think I understand why you are confused. You are not assigning to all elements in the array the value 2. Only to one element. See below:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @array;
$array[3] = 2;
print Dumper \@array;
__END__
$ perl test.pl
$VAR1 = [
undef,
undef,
undef,
2
];
BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.