Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: undef'ing @arrays caveat

by Kanji (Parson)
on Apr 20, 2002 at 00:40 UTC ( [id://160712]=note: print w/replies, xml ) Need Help??


in reply to undef'ing @arrays caveat

Since undef is a perfectly valid value for an element of an array, what your doing is no different than @array = ( undef ); sans the parens.

Also, while @array = () may do what you want, @array is still defined; it just evaluates as false when you test for truth (which you were).

To truly undefine @array you'd want to use undef @array; instead.

    --k.


Replies are listed 'Best First'.
Re: Re: undef'ing @arrays caveat
by dmmiller2k (Chaplain) on Apr 20, 2002 at 22:46 UTC

    Actually, arrays cannot be undefined, just made empty (same with hashes). If you try undef @array in the debugger and then dump its value using the 'x' command, the effect is the same as if you had said @array = (), namely 'empty array'. (The same may be said for hashes, which under the above circumstances, produce the message, 'empty hash').

    Only scalars may be truly undefined; undef is itself merely a value, albeit a special one. As a C/C++ programmer, I rather think of scalars analagous to pointers -- sort of "auto-allocating, auto-dereferencing, type-ambivalent pointers", but pointers nonetheless, with undef rather analagous to NULL.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime

      I was thinking of definedness in the defined sense, but after some testing I discovered something really bizarre:

      Calling defined on an array that was declared as empty versus one that had been populated and then later emptied returns different results!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found