Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

using undef on a array

by Anarion (Hermit)
on Oct 31, 2001 at 14:21 UTC ( [id://122314]=perlquestion: print w/replies, xml ) Need Help??

Anarion has asked for the wisdom of the Perl Monks concerning the following question:

Recently in a perl list someone ask how to clean an array, then i said @a=() or $#a=-1, but you can use undef @a too.
But i dont know what exatly do undef,heres and example to show what i mean:
perl -le 'use strict;my @a=("a");@a=();print "Defined" if defined @a;p +rint scalar @a'
Defined
0
perl -le 'use strict;my @a=("a");undef @a;print "Defined" if defined @ +a;print scalar @a'
Defined
0
perl -le 'use strict;my @a=("a");@a=undef;print "Defined" if defined @ +a;print scalar @a'
Defined
1

Its still avaliable and strict dont warn it, then its not undefined, perhaps it is only memory deallocated, i dont know ...
I read that i shouldnt use undef on arrays on perldoc -f defined but i want to know what undef do :)
$anarion=\$anarion;

Edit Masem - CODE tags

s==q^QBY_^=,$_^=$[x7,print

Replies are listed 'Best First'.
Re: using undef on a array
by tachyon (Chancellor) on Oct 31, 2001 at 15:09 UTC

    The usual way to use undef on an array is like this:

    @_ = (1,2,3); @_ = (); # this will empty array but this will undef @_; # empty array and allow reallocation memory print @_;

    Both lines shown remove all the values from @_. Underneath @_ = (); calls the C code void av_clear(AV*); whereas undef @_ calls void av_undef(AV*);

    The underlying C functions differ in that in the first case memory remains allocated for the array so if you just want to empty it so you can fill it again this is the way to go. In the second case memory should be deallocated and garbage collected. If you undef an array and then go to use it again you encounter the overhead involved with setting up any new array. There is a note somewhere in the docs(can't find it) noting that using undef to empty an array is inefficient compared to setting it to ()

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      This is the most accurate answer so far, IMO. I'd like to add some fairly minor points of clarification. The memory being free()d by undef @a is only the memory that the array itself uses to keep track of its elements. The memory allocated for the elements is free()d even when you just do @a= () (unless there are other references to elements elsewhere, in which case undef @a won't free that memory either).

      So the only time that it makes sense to undef @a is if @a previously contained a very large number of elements and you don't expect that it will again for a long time.

      Also, $#a= -1 is just an obfuscated way of writing @a= (), so please use the latter.

      Using defined on aggregates (arrays and hashes) is generally a very bad idea. Please don't do that unless you have very strange requirements and a good understanding of Perl internals.

              - tye (but my friends call me "Tye")
Re: using undef on a array
by davis (Vicar) on Oct 31, 2001 at 14:30 UTC
    This is because (I believe) the @a=undef code sets the first value of the array a to the undefined value - the array is still defined, as it contains one value, which happens to be 'undef'.
    I may be wrong about this, but I'm hoping not. :)
    Hope that helps
    davis
Re: using undef on a array
by blakem (Monsignor) on Oct 31, 2001 at 14:33 UTC
    I don't know the subtle differences between the first two (I use them interchangably) but the third one:
    @a = undef;
    is assigning a one element list to @a.... It's actually doing something like:
    @a = (undef);
    Which is a relatively common error.

    -Blake

      I think that the original problem was "how to clear out an array". (As usual, correct me if I'm wrong.)

      $#my_array=-1;


      f--k the world!!!!
      /dev/world has reached maximal mount count, check forced.

Re: using undef on a array
by davorg (Chancellor) on Oct 31, 2001 at 14:40 UTC
    @a = undef;

    This is interpreted as @a = (undef), i.e. it sets @a to a one element list, where the first (and only) element has the value undef.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: using undef on a array
by ajt (Prior) on Oct 31, 2001 at 14:42 UTC
    My humble understanding is:
    1. Here you create the array, then set all it's values to nothing, therefore the array is defined, but has no content
    2. In this case you undefine the array, so it's not defined any more and also has no content. It still exists, so you can still use it without error.
    3. As other have said here you're simply setting the first value of the array to an undef value.

    Interesting question.

      The last one is easy to understand, but if i try to asing a undef value to $#a it is not modified:

      perl -le 'use strict;my @a=("a");$#a=undef;print "Defined" if defined @a;print @a'
      Defined
      a

      But whats the meaning of defined, what exactly do the undef @a,what exatly perl do? If i write $#a=-1 i'm clearing all and have no values, the both, 1 and 2 have no content, thats why it prints that they have 0 elements, buf one its undefined, but still avaliable, sorry if i dont understand what it means with defined, you say it still exists ... in both cases exists and are emptys.

      $anarion=\$anarion;

      s==q^QBY_^=,$_^=$[x7,print

Re: using undef on a array
by wog (Curate) on Nov 01, 2001 at 01:10 UTC

    strict dont warn it

    Strict doesn't warn on anything; it just makes some things errors. However, if you had the -w switch on (or use warnings), then you'd notice, (update:) in recent versions of perl

    defined(@array) is deprecated at -e line 1. (Maybe you should just omit the defined()?)
    (This shows why you need more then just use strict.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2025-07-18 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.