The element
$dow[5] exists in the array since you've treated it as an lvalue and assigned to it when you did
$dow[5]=undef the second time you checked for existence. Why the first time checking for (
exists $dow[5]) returns you false is obviously because it had nothing assigned to it and that there's no implication if
$dow[6] does exist that
$dow[5] has to automatically exist.
Being defined is not related to existence, a variable can exist but it doesn't necessarily mean it is defined as is the case when you want to flush out a variable value.Another thing, a return value of undef can indicate many things, as operation failure, end of file...etc
Exists checks whether a variable is existing in the array, that is, it has a value associated with it as a result of assignment (i.e. $dow[0]=undef), this is clearly indicated in Perl documentation for Functions, check out both defined and exists functions to decloud the confusion
#!/usr/local/bin/perl
use strict;
my @dow=(); #array initialization is different from indexes initiali
+zation
$dow[0]=undef;
$dow[6]= "sat";
for(my $i=0; $i <=6;$i++){
if(!(defined($dow[$i]) || exists($dow[$i]))){print "\$dow[$i]
+\t not defined\n";}
if(exists($dow[$i]) && !defined($dow[$i])){ print "\$dow[$i]\t
+exists\t not defined\n"; }
if(exists($dow[$i]) && defined($dow[$i])){ print "\$dow[$i]\te
+xists\t defined\n" ;}
}
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
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.