Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to declare a variable???

Reply
Thread Tools

How to declare a variable???

 
 
leiji22@yahoo.com
Guest
Posts: n/a
 
      12-19-2005
I'd like to define a large hash at the end of the script file. The hash
contains some parameters.

But I need to use this hash variable at the beginning of the script
file.

The hash is very long, so it's impossible for me to put it at the
beginning of the script.

Do I need to declare it at the beginning of the file? If not, does it
matter where in the file I put the variable definition?

Thanks for any help!

 
Reply With Quote
 
 
 
 
Scott Bryce
Guest
Posts: n/a
 
      12-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'd like to define a large hash at the end of the script file. The hash
> contains some parameters.
>
> But I need to use this hash variable at the beginning of the script
> file.
>
> The hash is very long, so it's impossible for me to put it at the
> beginning of the script.
>
> Do I need to declare it at the beginning of the file? If not, does it
> matter where in the file I put the variable definition?
>
> Thanks for any help!
>


I'm not sure exactly what you are trying to accomplish. It looks to me
like you want to define a large data structure near the beginning of
your algorithm, but want to place the code that defines the data
structure near the end of your script for esthetic reasons.


my $large_data_structure = get_large_data_structure();

# Do something with the large data structure here
# $large_data_structure contains a reference to a hash.

sub get_large_data_structure {

my %large_data_structure = (

# Large data structure defined here.

}

return \%large_data_structure;
}
 
Reply With Quote
 
 
 
 
Scott Bryce
Guest
Posts: n/a
 
      12-19-2005
Scott Bryce wrote:

> sub get_large_data_structure {
>
> my %large_data_structure = (
>
> # Large data structure defined here.
>
> }

-------^

That should be a ).

>
> return \%large_data_structure;
> }

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      12-19-2005
(E-Mail Removed) wrote:
> I'd like to define a large hash at the end of the script file. The hash
> contains some parameters.
>
> But I need to use this hash variable at the beginning of the script
> file.
>
> The hash is very long, so it's impossible for me to put it at the
> beginning of the script.
>
> Do I need to declare it at the beginning of the file? If not, does it
> matter where in the file I put the variable definition?


You could use a subroutine to define it:

#!/usr/bin/perl
use warnings;
use strict;

my %large_hash = define_large_hash();

# many, many, many, many lines of code

sub define_large_hash {
key1 => value1,
key2 => value2,
....
key9998 => value9998,
key9999 => value9999,
}

__END__



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-19-2005
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) wrote:
> > I'd like to define a large hash at the end of the script file. The hash
> > contains some parameters.
> >
> > But I need to use this hash variable at the beginning of the script
> > file.
> >
> > The hash is very long, so it's impossible for me to put it at the
> > beginning of the script.
> >
> > Do I need to declare it at the beginning of the file? If not, does it
> > matter where in the file I put the variable definition?

>
> You could use a subroutine to define it:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my %large_hash = define_large_hash();
>
> # many, many, many, many lines of code
>
> sub define_large_hash {
> key1 => value1,
> key2 => value2,
> ...
> key9998 => value9998,
> key9999 => value9999,
> }
>
> __END__


Let me show up a few alternatives.

Almost equivalently, the constant pragma can be used:

my %large_hash = LARGE_HASH;

# many, many, many, many lines of code

use constant LARGE_HASH => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);

Using Eric Roode's Readonly (from CPAN) would currently require an
explicit BEGIN block to make sure %large_hash is set early:

my %large_hash;

# many, many, many, many lines of code

use Readonly;
BEGIN {
Readonly %large_hash => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);
}


As of the next version (if I understand correctly) one will be able
to write

my %large_hash;

# many, many, many, many lines of code

use Readonly \ %large_hash => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);

Either way, I notice that with Readonly the declaration "my %large_hash"
would *have* to be commented to the effect that it is set at compile time
further down in the code. That is the cost of saving the intermediate
routine define_large_hash (or LARGE_HASH) of the other solutions, which
make the assignment explicit.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-19-2005
Jim Gibson <(E-Mail Removed)> wrote in
news:191220051007403589%(E-Mail Removed):

> In article <do6ggt$bne$(E-Mail Removed)-Berlin.DE>, Anno Siegel
> <(E-Mail Removed)-berlin.de> wrote:
>

....

>> Using Eric Roode's Readonly (from CPAN) would currently require an
>> explicit BEGIN block to make sure %large_hash is set early:
>>
>> my %large_hash;
>>
>> # many, many, many, many lines of code
>>
>> use Readonly;
>> BEGIN {
>> Readonly %large_hash => (
>> key1 => value1,
>> key2 => value2,
>> ...
>> key9998 => value9998,
>> key9999 => value9999,
>> );
>> }
>>

>
> Why use Readonly? Isn't simpler better?


If the hash is going to be used as a lookup table, it might make sense
to prevent it from being changed.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      12-19-2005
(E-Mail Removed) wrote:
> I'd like to define a large hash at the end of the script file. The hash
> contains some parameters.
>
> But I need to use this hash variable at the beginning of the script
> file.
>
> The hash is very long, so it's impossible for me to put it at the
> beginning of the script.


How does its size make it impossible to put at the beginning, but possible
to put it at the end?

Anyway, I often do things like this by putting the data in a __DATA__
section, and near the start script reading it into a hash with a "while
(<DATA>)" loop.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-19-2005
Jim Gibson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <do6ggt$bne$(E-Mail Removed)-Berlin.DE>, Anno Siegel
> <(E-Mail Removed)-berlin.de> wrote:
>
> > > (E-Mail Removed) wrote:
> > > > I'd like to define a large hash at the end of the script file. The hash
> > > > contains some parameters.
> > > >
> > > > But I need to use this hash variable at the beginning of the script
> > > > file.

>
>
> >
> > Let me show up a few alternatives.
> >
> > Almost equivalently, the constant pragma can be used:
> >
> > my %large_hash = LARGE_HASH;
> >
> > # many, many, many, many lines of code
> >
> > use constant LARGE_HASH => (
> > key1 => value1,
> > key2 => value2,
> > ...
> > key9998 => value9998,
> > key9999 => value9999,
> > );
> >
> > Using Eric Roode's Readonly (from CPAN) would currently require an
> > explicit BEGIN block to make sure %large_hash is set early:
> >
> > my %large_hash;
> >
> > # many, many, many, many lines of code
> >
> > use Readonly;
> > BEGIN {
> > Readonly %large_hash => (
> > key1 => value1,
> > key2 => value2,
> > ...
> > key9998 => value9998,
> > key9999 => value9999,
> > );
> > }
> >

>
> Why use Readonly? Isn't simpler better?


Often. We know too little about how %large_hash is used to tell. I
happened to be interested in how a Readonly solution looks like. I don't
think the OP said explicitly that %large_hash wouldn't be changed, I just
assumed it for convenience.

Then again, I do try to avoid BEGIN blocks when I can by piggy-backing
compile-time action on a use-statement. "BEGIN" looks somewhat alarming,
an everyday "use" is easier on the reader's nerves. The "BEGIN"-solution
*is* simple in being direct, but using a module *looks* simpler. That's
what information hiding is about.

> my %large_hash;
>
> # many, many, many, many lines of code
>
> BEGIN {
> %large_hash = (
> key1 => value1,
> key2 => value2,
> ...
> key9998 => value9998,
> key9999 => value9999,
> );
> }


Sure. Like the Readonly solutions that needs a comment where %large_hash
is declared/first used.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
leiji22@yahoo.com
Guest
Posts: n/a
 
      12-20-2005

Thanks so much for the alternative solutions!

Very helpful. Have a good holiday season, guys.

 
Reply With Quote
 
Ian Stuart
Guest
Posts: n/a
 
      12-22-2005
A. Sinan Unur wrote:
> If the hash is going to be used as a lookup table, it might make sense
> to prevent it from being changed.

Actually, I've come across a few situations where I've wanted to be able
to have a large lookup-table with all the common elements in it, yet
also be able to add some specific "extras" for specific tasks.

For example: MathML::Entities contains some 2,000 XML+XHTML entities
(&foo and the number-code for that entity ('aacute' => '&#x000E1;').
This is good for 90% of cases, however I have an XML dataset created
from 1980's data, with entities such as &dotlessi; - which are not part
of the XML+XHTML standard.
This means that, to manage this data, I need to extend the
MathML::Entities lookup-table - not possible if it's either a 'my'
variable, locked as a read-only hash, or without a method for adding
records...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do I declare subpackages? Derek Graham VHDL 7 09-18-2004 06:20 PM
How do we declare a signed integer? Kwaj VHDL 2 05-06-2004 03:07 PM
Error: Must Declare Variable Boris Zakharin ASP .Net 2 07-22-2003 01:04 AM
how to declare session variable in global.asax file khawar ASP .Net 1 07-10-2003 08:03 PM



Advertisments