Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Adding items to arrays: best approach?

by AnomalousMonk (Archbishop)
on May 28, 2020 at 16:23 UTC ( [id://11117426]=note: print w/replies, xml ) Need Help??


in reply to Adding items to arrays: best approach?

Further to davido's discussion of the Perl interpreter's efforts to manage memory for your arrays (and hashes and everything else):
geertvc: Note that if you have an array that starts small and that will grow to a very large size that you know (or can estimate) in advance, it's possible and occasionally advantageous to "pre-grow" the array by assignment to the array's max index via the  $# array dereference operator (if that's the correct terminology).

c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(a b c); print 'A: array max index: ', $#ra; ;; $#ra += 1_000_000; print 'B: array maximum index: ', $#ra; print 'B: number of array elements: ', scalar @ra; " A: array max index: 2 B: array maximum index: 1000002 B: number of array elements: 1000003
See perldata regarding $#. In most cases, however, it's best to let the interpreter manage these things on its own.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

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

    No recent polls found