Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How do I completely empty out an array?

by ysth (Canon)
on Nov 04, 2003 at 07:32 UTC ( [id://304348]=note: print w/replies, xml ) Need Help??


in reply to How do I completely empty out an array?

Many different ways, often differing in how much memory perl holds on to. I'm going to try not to be too technical, so I will probably end up not pedantically correct.

  • undef(@array) frees all the memory associated with the array except the bare minimum, as if @array had been declared but never used.
  • splice(@array), $#array = -1, and @array = () all empty the array, but hold on to all the indices in case they are used again. There may be slight technical differences between some of these.
  • for (@array) { undef $_ } (or the equivalent C-like:
    for ($i = 0; $i<=$#array; ++$i) { undef $array[$i]; }
    leaves the length of the array alone (so scalar(@array) and $#array are unchanged) but blows away all the contents, including any string buffers they might have used.
  • for (@array) { $_ = undef } (or the equivalent C-like:
    for ($i = 0; $i<=$#array; ++$i) { $array[$i] = undef; }
    also leaves the length of the array alone (so scalar(@array) and $#array are unchanged) but undefines all the contents. However, if $array[42] was "what is the meaning of life", $array[42] will still have a 28+ byte area reserved for a string so assigning $array[42]="python's flying circus" will not require perl to allocate any new memory.
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2024-04-18 05:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found