returning a value from a hash using a reference

W

wana

I know I must be doing something stupid here. I want to return a
value out of a hash using a reference. To be specific:
$session{_session_id} is the hash value I need to get at. Just to
make things interesting, I named my scalar which refers to %session
with the same name: $session = \%session

I know I could use -> notation, but I thought this would work:

$%$session{_session_id}

or

$$session{_session_id}

or some other variation using appropriate parentheses.

Is it possible to dereference this reference in this way to get at the
value?
 
G

Gunnar Hjalmarsson

wana said:
I know I must be doing something stupid here. I want to return a
value out of a hash using a reference. To be specific:
$session{_session_id} is the hash value I need to get at. Just to
make things interesting, I named my scalar which refers to %session
with the same name: $session = \%session

I know I could use -> notation, but I thought this would work:

$%$session{_session_id}

or

$$session{_session_id}

or some other variation using appropriate parentheses.

my %session = (session_id => 123);
my $session = \%session;

# Accessing the hash value directly:
print $session{session_id}, "\n";

# Dereferencing the hash reference:
print ${$session}{session_id}, "\n";
# or
print $session->{session_id}, "\n";
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top