Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: scoping large arrays - newbie Q

by moof1138 (Curate)
on Jun 18, 2002 at 04:40 UTC ( #175268=note: print w/ replies, xml ) Need Help??


in reply to Re: scoping large arrays - newbie Q
in thread scoping large arrays - newbie Q

Thank you for the response. I do know about using references, I have used them here and there to pass something to a function that was not in that function's scope. That's not really what I am looking for here. I was not clear enough. I just really wonder whether it is more efficient to set up my array at point A, or point B in the pseudocode below which is trying illustrate my question:

#!/usr/bin/perl -w use strict; my @bigarray = ("insert", "a very", "long list here");#point A my $thing1 = mySub(); my $thing2 = mySub(); #...etc - using mySub repeatedly sub mySub { #point B - should I declare my @bigarray here instead, and why? for (@bigarray){ #do stuff with array } }


Comment on Re: Re: scoping large arrays - newbie Q
Download Code
(jeffa) 3Re: scoping large arrays - newbie Q
by jeffa (Chancellor) on Jun 18, 2002 at 05:39 UTC
    With the example you have given, i say point A, because if you declare your array at point B, you will re-declare it as many times as you call mySub(). Also, consider passing @bigarray to mySub() as a reference. Just be sure to declare mySub() before @bigarray, otherwise @bigarray is accessible by mySub():
    use strict; sub mySub { my $ref = shift; for (@$ref) { #do stuff with array } } my @bigarray = ("insert", "a very", "long list here"); my $thing1 = mySub(\@bigarray); my $thing2 = mySub(\@bigarray);
    If the array in question is only pertinent to the subroutine, and either that sub will only be called once or the array will change with each sub call, then declare the array inside the subroutine.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Re: Re: scoping large arrays - newbie Q
by samtregar (Abbot) on Jun 18, 2002 at 07:06 UTC
    Can I choose point C? Here's an alternative:

    { my @bigarray = ("insert", "a very", "long list here"); # c sub mySub { for (@bigarray){ # do stuff with array } } }

    This keeps @bigarray private to the subroutine but only initializes it once. You get the best of both worlds at no added cost!

    -sam

      I second that.

      -Lee

      "To be civilized is to deny one's nature."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-08-30 19:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls