Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Difference between exists and defined

by thanos1983 (Parson)
on Apr 16, 2019 at 09:37 UTC ( #1232633=note: print w/replies, xml ) Need Help??


in reply to Difference between exists and defined

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!

Replies are listed 'Best First'.
Re^2: Difference between exists and defined
by Veltro (Hermit) on Apr 16, 2019 at 10:13 UTC

    Your code example, the way you use Data::Dumper to show the contents of the array allows room for misinterpretation of what is realy happening here. I feel that a better code example is the following:

    use strict ; use warnings ; use Data::Dumper ; my @ar ; $ar[0] = undef ; $ar[2] = 3 ; if ( exists $ar[0] ) { print "0 exists\n" ; } if ( exists $ar[1] ) { print "1 exists\n" ; } if ( exists $ar[2] ) { print "2 exists\n" ; } if ( exists $ar[3] ) { print "3 exists\n" ; } print Dumper(\@ar) ;

    This prints:

    0 exists 2 exists

    As you can see element 0 DOES exists when it is explicitly set to undefined. Using Data::Dumper prints:

    $VAR1 = [ undef, undef, 3 ];

      Hello Veltro,

      You are right my example was not clear if I do not add the following part:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my @array; $array[0] = undef; # Assigns the value 'undef' to $array[0] $array[3] = 2; # print Dumper \@array; for my $value (@array) { say "Defined: \$array[$value]" if defined $value; } __END__ $ perl test.pl Defined: $array[2]

      The reason that this is happening is explained in the documentation exists:

      Given an expression that specifies an element of a hash, returns true +if the specified element in the hash has ever been initialized, even +if the corresponding value is undefined.

      So if you check the array element with exists and you have manually defined undef to the element then exists will return True. :)

      Thanks for pointing it out. Hopefully it will avoid confusion for future reference. :)

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1232633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-05-25 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (143 votes). Check out past polls.

    Notices?