Closed Thread Icon

Topic awaiting preservation: Defining Global Arrays Pages that link to <a href="https://ozoneasylum.com/backlink?for=12440" title="Pages that link to Topic awaiting preservation: Defining Global Arrays" rel="nofollow" >Topic awaiting preservation: Defining Global Arrays\

 
Author Thread
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-22-2002 05:23

I want to define a series of arrays and I need them to be globaly set, but I can only determine if they are necessary from deep inside of a function...

isn't there something like-

my global @myarray;

-in Perl?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Piper
Paranoid (IV) Inmate

From: California
Insane since: Jun 2000

posted posted 09-22-2002 05:41

Hi Petskull,

I set my globals a couple of different ways. I will set them at the top of the script outside of any functions like this:

code:
my @ARRAY = [];

sub main {
@ARRAY = foo();
}

sub foo {
my @data = [1, 2, 3, 4];
return @data
}



or I will set them in a vars pragma like:

code:
use vars qw/$IN $DB $CFG @ARRAY/;

sub main {
...
}



The vars pragma is considered somewhat obsolete now but I think it makes for cleaner code.

Regards,
Charlie

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-22-2002 06:37

see, the thing is that I will end up defining a LOT of arrays that I don't need... I think that by defining only the arrays I'm going to use- which may be only a small fraction of the total arrays I would set up otherwise- I would save on cpu and mem usage, leaving me with faster and smoother code that's one step closer to optimization..


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Piper
Paranoid (IV) Inmate

From: California
Insane since: Jun 2000

posted posted 09-24-2002 06:09

How about using a single hasref of arrays? You would just add your arrays to the hasref as needed. Take a look at this:

code:
#!/usr/bin/perl -w
use strict;
use vars qw/$HASHREF_OF_ARRAYS/; # define the global hashref


foo();
bar();
baz();


# set one array
sub foo {
$HASHREF_OF_ARRAYS->{array1} = [0, 1, 2, 3];
}


# set another array
sub bar {
$HASHREF_OF_ARRAYS->{array2} = ['a', 'b', 'c', 'd'];
}


# print all of the arrays in the hahref
sub baz {
print "Content-Type: text/html\n\n";
print 'Printing arrays in the hashref...<br><br>';

foreach my $array_in_hasref ( keys %$HASHREF_OF_ARRAYS ) {
print "$array_in_hasref: @{$$HASHREF_OF_ARRAYS{$array_in_hasref}}<br>"
}
}



~Charlie



[This message has been edited by Piper (edited 09-24-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu