Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Difference between exists and defined

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

in reply to Re^2: Difference between exists and defined
in thread Difference between exists and defined

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 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?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2021-04-17 05:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found