in reply to Design Question

Well you could create the array first as a global variable (to the package) then just manipulate it. Something like this:

my @array = (); sub processA { push(@array,$some_data); common_stuff(); # clear the array? } sub processB { for(1 .. 1000) { push(@array,$some_data); } common_stuff(); # clear the array? } sub common_stuff { foreach(@array) { #do stuff ... } }

With that you should always have just one array that gets no bigger than the largest amount of stuff you put in it. Of course it also depends on what you're doing with the data and weither or not you clear your array.

Lobster Aliens Are attacking the world!

Replies are listed 'Best First'.
Re: Re: Design Question
by David Caughell (Monk) on Aug 07, 2003 at 09:02 UTC

    I'm partially through chapter 2 of PP, so I thought that I'd add something to this:

    Since your common array is going to have 1000 values in it, you may want to define you 1000th value before you start adding stuff to the array, in order to speed up execution time.

    my @arr(); $arr[999] = ''; # or perhaps: $#arr = 999; #... insert code suggested by other perlmonks here

    I'm not sure if 1000 entries on an array is enough to get a speed boost from predefining it's length, but it likely would...

    Also, consider compiling to bytecode or doing a memory dump and saving it as an executable file (if it doesn't have to travel across operating systems).

    Take care,
    Dave.

    "For fate which has ordained that there shall be no friendship among the evil has also ordained that there shall ever be friendship among the good." - Plato / Socrates