From: Frank Seitz on
Uri Guttman wrote:
>>>>>> "FS" == Frank Seitz <devnull4711(a)web.de> writes:
>
> FS> Uri Guttman wrote:
> >>>>>>> "j" == jidanni <jidanni(a)jidanni.org> writes:
> >>
> j> How can I write
> j> print $F[4] if exists $F[4];
> j> even simpler?
> j> print if exists for $F[4];
> j> print if exists $_ for $F[4];
> j> don't work.
> >>
> >> well, exists is for checking if a key exists in a hash. you don't have
> >> any hashes there so of course it won't work.
>
> FS> No, exists() checks array elements, too. See perdoc -f exists.
>
> even so, it is a useless concept IMO.

This is a completely different point.

> array elements can be never
> allocated or filled with undef and there is little reason to know the
> difference. this is unlike knowing if a key exists regardless of its
> value being set with undef or never being set. exists was changed to
> support arrays because of pseudohashes which now are obsolete and
> removed from perl. maybe they should remove the array side of exists
> too.

Here is a simple use case:

my @a;
$a[1] = 4711;
$a[4] = 4712;

for (my $i = 0; $i < @a; $i++) {
printf "%d %s\n",$i,exists $a[$i]? 'filled': 'empty';
}
__END__
0 empty
1 filled
2 empty
3 empty
4 filled

Frank
--
Dipl.-Inform. Frank Seitz
Anwendungen f�r Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Blog: http://www.fseitz.de/blog
XING-Profil: http://www.xing.com/profile/Frank_Seitz2
From: RedGrittyBrick on
On 04/03/2010 19:46, jidanni(a)jidanni.org wrote:
> How can I write
> print $F[4] if exists $F[4];
> even simpler?
> print if exists for $F[4];
> print if exists $_ for $F[4];
> don't work.

print $F[4];

As in
perl -e '$F[2]=2; $F[4]=4; for $i (1..5) {print $F[$i];}'
24


--
RGB
From: Eric Pozharski on
with <87ocj3bmdn.fsf(a)quad.sysarch.com> Uri Guttman wrote:
>>>>>> "FS" == Frank Seitz <devnull4711(a)web.de> writes:
*SKIP*
> FS> No, exists() checks array elements, too. See perdoc -f exists.

And that entry is somewhat strange (underdeveloped?).

*SKIP*
> exists was changed to support arrays because of pseudohashes which now
> are obsolete and removed from perl. maybe they should remove the array
> side of exists too.

I doubt that would ever happen ('breaking old code' spell). Although:

% perl -wle '
$x[25] = "";
print "a$x[4]a" if exists $x[4];
print "b$x[4]b" if defined $x[4];
print "c$x[4]c" if $x[4];
print "d$x[4]d"
__END__
'
Use of uninitialized value $x[4] in concatenation (.) or string at -e line 6.
dd

Me smells XY-problem.

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
From: Ben Morrow on

Quoth Frank Seitz <devnull4711(a)web.de>:
> Uri Guttman wrote:
>
> > array elements can be never
> > allocated or filled with undef and there is little reason to know the
> > difference. this is unlike knowing if a key exists regardless of its
> > value being set with undef or never being set. exists was changed to
> > support arrays because of pseudohashes which now are obsolete and
> > removed from perl. maybe they should remove the array side of exists
> > too.

There was some discussion of this recently, with the usual fear of
breaking something relying on current behaviour. What prompted the
discussion is that 5.12 will allow 'keys' on arrays, so we get

~% perl5.12.0 -E'$a[1]=1; say for exists %a[0], keys @a'

0
1
~%

which is a little inconsistent.

> Here is a simple use case:
>
> my @a;
> $a[1] = 4711;
> $a[4] = 4712;
>
> for (my $i = 0; $i < @a; $i++) {
> printf "%d %s\n",$i,exists $a[$i]? 'filled': 'empty';

WRONG. You mean 'defined'. Whether an element responds to 'exists' or
not depends on whether memory has been allocated to hold that element
yet, which is not generally something you can predict.

Ben

From: Frank Seitz on
Ben Morrow wrote:
> Quoth Frank Seitz <devnull4711(a)web.de>:
>>
>> my @a;
>> $a[1] = 4711;
>> $a[4] = 4712;
>>
>> for (my $i = 0; $i < @a; $i++) {
>> printf "%d %s\n",$i,exists $a[$i]? 'filled': 'empty';
>
> WRONG. You mean 'defined'. Whether an element responds to 'exists' or
> not depends on whether memory has been allocated to hold that element
> yet, which is not generally something you can predict.

In my understanding an array element "exists", when I assigned a
value to it, otherwise not.

my @a;
$a[1] = 4711;
$a[3] = undef;
$a[4] = 4712;

print "exists:\n";
for (my $i = 0; $i < @a; $i++) {
printf " %d %s\n",$i,exists $a[$i]? 'filled': 'empty';
}
print "defined:\n";
for (my $i = 0; $i < @a; $i++) {
printf " %d %s\n",$i,defined $a[$i]? 'filled': 'empty';
}
__END__
exists:
0 empty
1 filled
2 empty
3 filled
4 filled
defined:
0 empty
1 filled
2 empty
3 empty
4 filled

I get what I expect. Where is the problem?

Frank
--
Dipl.-Inform. Frank Seitz
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Blog: http://www.fseitz.de/blog
XING-Profil: http://www.xing.com/profile/Frank_Seitz2