Syntax for a slice of a hashref

D

David Filmer

I want to do something like this:

$hash_ref->{'key1', 'key2'} = @two_things;

but I wind up with:

$hash_ref{'key1key2'} == 2

A couple of other guesses were also unsuccessful.

What is the proper syntax for this?

Thanks!
 
J

J. Gleixner

David said:
I want to do something like this:

$hash_ref->{'key1', 'key2'} = @two_things;

but I wind up with:

$hash_ref{'key1key2'} == 2

A couple of other guesses were also unsuccessful.

What is the proper syntax for this?

my $hash_ref;
@$hash_ref{ 'a', 'b' } = ( 1, 2 );
use Data::Dumper;
print Dumper $hash_ref;


$VAR1 = {
'a' => 1,
'b' => 2
};
 
X

xhoster

David Filmer said:
I want to do something like this:

$hash_ref->{'key1', 'key2'} = @two_things;

but I wind up with:

$hash_ref{'key1key2'} == 2

A couple of other guesses were also unsuccessful.

What is the proper syntax for this?

Alas, there is no arrow syntax for slices. You have
to use the sigil syntax:

@{$hashref}{'key1', 'key2'} = @two_things;

which gets ugly when the actual ref is not a simple scalar.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Tad McClellan

David Filmer said:
I want to do something like this:

$hash_ref->{'key1', 'key2'} = @two_things;

but I wind up with:

$hash_ref{'key1key2'} == 2

A couple of other guesses were also unsuccessful.

What is the proper syntax for this?


Apply "Use Rule 1" from perlreftut.pod.

I like to use 3 steps:

1) @hash{'key1', 'key2'} = @two_things; # pretend it is a plain hash

2) @{ }{'key1', 'key2'} = @two_things; # replace the name with a block

# fill in the block with the appropriate type of reference
3) @{$hash_ref}{'key1', 'key2'} = @two_things;
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top