Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash reference question

Reply
Thread Tools

Hash reference question

 
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      08-08-2004
I'm defining a hash similiar to what follows:

my $HASH = (
'key1' => (
'sub1' => 'key1value1',
'sub2' => 'key1value2'
),
'key2' => (
'sub1' => 'key2value1',
'sub2' => 'key2value2')
)

and I would like to pass the reference of $HASH{'key1'} to a sub. How
do I go about doing this?

Thanks!

--
Kristofer Pettijohn
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
ChrisO
Guest
Posts: n/a
 
      08-08-2004
Kristofer Pettijohn wrote:
> I'm defining a hash similiar to what follows:
>
> my $HASH = (
> 'key1' => (
> 'sub1' => 'key1value1',
> 'sub2' => 'key1value2'
> ),
> 'key2' => (
> 'sub1' => 'key2value1',
> 'sub2' => 'key2value2')
> )
>
> and I would like to pass the reference of $HASH{'key1'} to a sub. How
> do I go about doing this?
>


First of all, define the hash properly using matching braces and not
parens and end with a semicolon:

my $hash = {
key1 => {
sub1 => 'key1value1',
sub2 => 'key1value2',
},
key2 => {
sub1 => 'key2value1',
sub2 => 'key2value2',
},
};

The quoting of the keys is not required unless you are going to include
whitespace in your key values.

To pass the key1 reference to a sub, just reference it:

subroutine( $hash->{key1} );
sub subroutine { print "$_[0]->{sub1}\n" }

-ceo
 
Reply With Quote
 
 
 
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      08-08-2004
ChrisO <(E-Mail Removed)> wrote:
> First of all, define the hash properly using matching braces and not
> parens and end with a semicolon:


I do -- I guess I made the mistake of using parens instead of braces..
but the semicolons are definitely in the code -- I just typed up a short
un-tested example for my question.

Thanks for your help though - I understand it now.

--
Kristofer Pettijohn
(E-Mail Removed)
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-09-2004
>>>>> "SM" == Steve May <(E-Mail Removed)> writes:

>> The quoting of the keys is not required unless you are going to
>> include whitespace in your key values.
>>


SM> Ah.... that is not totally correct. When warnings are on and using
SM> strict, a key like this-is-a-key will *not* compile.

he should have said when using => you don't need to quote simple word
keys (a leading - is allowed because too many packages use -foo as
args). pretty much anything else needs normal quoting.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
ChrisO
Guest
Posts: n/a
 
      08-09-2004
Steve May wrote:

> ChrisO wrote:
>
>> Kristofer Pettijohn wrote:
>>
>>> I'm defining a hash similiar to what follows:
>>>
>>> my $HASH = (
>>> 'key1' => (
>>> 'sub1' => 'key1value1',
>>> 'sub2' => 'key1value2'
>>> ),
>>> 'key2' => (
>>> 'sub1' => 'key2value1',
>>> 'sub2' => 'key2value2')
>>> )
>>>
>>> and I would like to pass the reference of $HASH{'key1'} to a sub. How
>>> do I go about doing this?
>>>

>>
>> First of all, define the hash properly using matching braces and not
>> parens and end with a semicolon:
>>
>> my $hash = {
>> key1 => {
>> sub1 => 'key1value1',
>> sub2 => 'key1value2',
>> },
>> key2 => {
>> sub1 => 'key2value1',
>> sub2 => 'key2value2',
>> },
>> };
>>
>> The quoting of the keys is not required unless you are going to
>> include whitespace in your key values.
>>

>
> Ah.... that is not totally correct. When warnings are on and using
> strict, a key like this-is-a-key will *not* compile.
>


I'm not surprised. When I said "whitespace", I had some faint
recollection of dashes or underscores not working as you pointed out.
But since I never code without "use warnings qw( all );" and "use
strict;" I don't find much occasion to trip over those kinds of things.
I see these two pragmas pretty religiously recommended here in c.l.p.m
and for good reason.

-ceo
 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Combining multiple hash references into one hash reference Arvin Portlock Perl Misc 6 09-02-2005 07:18 PM
copying values from a hash into CGI.pm via tied hash reference ioneabu@yahoo.com Perl Misc 14 01-10-2005 01:22 PM
hash reference as a hash key Steve Perl Misc 6 09-28-2003 10:49 AM



Advertisments