## Re: Can zero and undef be distinguished?

Expand Messages
• ... elements. ... Maybe something like this: for (@array[0..3]) { if (defined) { ...do whatever } else { ...\$_ is undefined } } Chris
Message 1 of 5 , Jul 9, 2007
wrote:
>
>
> Hello All:
>
> I have an array, @array = (0, undef, 2, undef);
> And I want different operations on numerical and undef array
elements.
>
> Since numerically undef behaves as zero, but undef are strings of
length zero, I did the following to distinguish between the two:
>
> \$len = length(\$array[\$i]) # where \$i-> 0 - 3
> if (\$len)
> {
> # the array element is numerical
> }
> else
> {
> # the array element is undef
> }
>
> This works. However, this also returns the warning (though
expectedly):
> "Use of initialized value in length at line..."
>
> Is there a better way to do so?
>
> Thank You!
>

Maybe something like this:

for (@array[0..3]) {
if (defined) {
...do whatever
}
else {
...\$_ is undefined
}
}

Chris
• To: perl-beginner@yahoogroups.com From: aditi gupta Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
Message 2 of 5 , Jul 9, 2007
To: perl-beginner@yahoogroups.com
Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
Subject: [PBML] Can zero and undef be distinguished?

>
> Hello All:
>
> I have an array, @array = (0, undef, 2, undef);
> And I want different operations on numerical and undef array elements.

perldoc -f defined

Jenda
===== Jenda@... === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
• Thank You Chris and Jenda!! :) Jenda Krynicky wrote: To: perl-beginner@yahoogroups.com From:
Message 3 of 5 , Jul 9, 2007
Thank You Chris and Jenda!! :)

Jenda Krynicky <Jenda@...> wrote: To: perl-beginner@yahoogroups.com
Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
Subject: [PBML] Can zero and undef be distinguished?

>
> Hello All:
>
> I have an array, @array = (0, undef, 2, undef);
> And I want different operations on numerical and undef array elements.

perldoc -f defined

Jenda
===== Jenda@... === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

---------------------------------
Heres a new way to find what you're looking for - Yahoo! Answers

[Non-text portions of this message have been removed]
Your message has been successfully submitted and would be delivered to recipients shortly.