John W. Krahn wrote:
> Rob Dixon wrote:
>> loody wrote:
>>>> if( exists $data{$key} ){
>>>> print "\t\$data{$key} exists\n";
>>> thanks for your kind help.
>>> Could I get the conclusion that exists is only used for determining
>>> the element of hash and arrays?
>>> appreciate your help,
>> Consider this program.
>>
>> use strict;
>> use warnings;
>>
>> my @array;
>> $array[0] = 'a';
>> $array[2] = undef;
>>
>> my %hash;
>> $hash{a} = 1;
>> $hash{c} = undef;
>>
>> Now,
>>
>> exists $array[1] and exists $hash{b} will be false.
>>
>> exists $array[2] and exists $hash{c} will be true.
>>
>> defined $array[1], defined $array[2], defined $hash{b}, and defined $hash{c}
>> will all be false.
>>
>> So an array or hash element may exist but have an undefined value. defined()
>> will return true only if the elements exists and has a defined value.
>> exists()
>> will return true if the element exists, regardless of its value.
>>
>> Note also that undef $array[0] and undef $hash{a} leave the element in
>> existence
>> but make its value undefined, while delete $array[0] and delete $hash{a} make
>> the element non-existent.
>
> Incorrect, delete does not remove array elements:
>
> $ perl -le'use Data::Dumper; my @a = "a".."d"; delete $a[1]; print
> Dumper [EMAIL PROTECTED]'
> $VAR1 = [
> 'a',
> undef,
> 'c',
> 'd'
> ];
According to exists() it does.
Rob
use strict;
use warnings;
my @x = qw/a b c/;
delete $x[1];
print "doesn't exist\n" unless exists $x[1];
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/