Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: How to differentiate an empty array from an unitialized one?

by haukex (Archbishop)
on Jul 09, 2018 at 09:29 UTC ( [id://1218150]=note: print w/replies, xml ) Need Help??


in reply to Re: How to differentiate an empty array from an unitialized one?
in thread How to differentiate an empty array from an unitialized one?

@array has a defined value, period. ... therefore @array is not undefined. ... Even split ",", undef; returns an empty list, which is a defined value.

Sorry, this is incorrect, arrays don't have a concept of "(un)defined", only the elements of an array do - an array is either empty, or not empty. Whether or not the elements of an array are undef or not is a different question - even an array of one undef element is considered "true" by Perl because the array is non-empty.

$ perl -wMstrict -Mdiagnostics -le 'my @array; print defined(@array)' Can't use 'defined(@array)' (Maybe you should just omit the defined()? +) at -e line 1 (#1) (F) defined() is not useful on arrays because it checks for an undefined scalar value. If you want to see if the array is empty, just use if (@array) { # not empty } for example. Uncaught exception from user code: Can't use 'defined(@array)' (Maybe you should just omit the define +d()?) at -e line 1. $ perl -wMstrict -le 'my @array=(undef); print @array?"true":"false"' true
Once you execute my @array =  split ( "," , $_ );, @array has a defined value, period.
$ perl -wMstrict -MData::Dump -e 'my @array; dd @array; $_=""; @array = split(",",$_ ); dd @array' () ()
If split could return undef ... but split doesn't do that.
$ perl -wMstrict -MData::Dump -e 'dd split /-|(x)/, "-", -1' ("", undef, "")

Replies are listed 'Best First'.
Re^3: How to differentiate an empty array from an unitialized one?
by dsheroh (Monsignor) on Jul 10, 2018 at 07:58 UTC
    arrays don't have a concept of "(un)defined"
    Yeah, that's an odd design decision which I personally disagree with. I see no logical reason to distinguish between "empty" and "undefined" for scalars, but not for arrays.

    In any case, I considered going into that in my previous comment, but obviously decided against it. I probably should have, given that the literal answer to "How to differentiate an empty array from an uninitialized one?" is "You don't, because Perl doesn't."

    $ perl -wMstrict -MData::Dump -e 'dd split /-|(x)/, "-", -1' ("", undef, "")
    Interesting. I wasn't aware of that behavior in split. Thanks for pointing it out!
      > no logical reason to distinguish between "empty" and "undefined" for scalars

      What do you mean by "empty scalar"? The empty string? Note that it's a perfect value which you can print, measure its length, match against a regex, etc. Undefined value, on the other hand, means the value is missing. If you need to have a special value for "missing value" for the whole array, use an array reference, and assign undef if the whole array is "undefined".

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        You cut off the quote at an unfortunate place. I see plenty of reasons for scalars to distinguish between empty (string) and undefined. What I see no logical reason for is that scalars make that distinction and arrays don't. It makes obvious sense to either have undef for both or have undef for neither, but what's the reasoning behind only doing it for one and not the other?

        Anyhow, as you said, you can definitely work around it using references, no problem there. The way Perl implements it is perfectly usable. It just seems strange to me that scalars have a special "missing value" value, but arrays don't.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found