Hash reference question

  • Thread starter Kristofer Pettijohn
  • Start date
K

Kristofer Pettijohn

I'm defining a hash as 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!
 
G

Gunnar Hjalmarsson

Kristofer said:
I'm defining a hash as follows:

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

Please "use warnings;"! If you had done so, you'd understand that you
should use braces instead of parentheses.
and I would like to pass the reference of $HASH{'key1'} to a sub.

Since there is no %HASH, there is no $HASH{'key1'} either. Maybe you
mean $$HASH{key1} or $HASH->{key1}.
How do I go about doing this?

mysub( $HASH->{key1} );

You have some reading to do. Start here:

perldoc perlreftut
perldoc perldsc

Good luck!
 
N

nobull

I'm defining a hash as follows:

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

That is a eqivalent to

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

Somehow I think you meant to say:

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.

Do you mean the reference that is stored _in_ $HASH{key1} or a
reference _to_ the scalar storage location $HASH{key1} (i.e.
\$HASH{key1} ) ?
How do I go about doing this?

In either case the answer is - just do it.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
N

nobull

That is a eqivalent to

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

Or just

my $HASH = 'key2value2';

Sorry I read $HASH as %HASH. Funny thing, the power of suggestion.
Somehow I think you meant to say:
[ snip gibberish ]
my $HASH = {
'key1' => {
'sub1' => 'key1value1',
'sub2' => 'key1value2',
},
'key2' => {
'sub1' => 'key2value1',
'sub2' => 'key2value2'.
},
};

or

my %HASH = (
'key1' => {
'sub1' => 'key1value1',
'sub2' => 'key1value2',
},
'key2' => {
'sub1' => 'key2value1',
'sub2' => 'key2value2'.
},
);
This newsgroup does not exist

So at least I can take comfort from the fact that few people will see
this bit of idiocy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top