Syntax for a slice of a hashref

Syntax for a slice of a hashref

am 15.11.2007 22:58:33 von 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!

--
David Filmer (http://DavidFilmer.com)

Re: Syntax for a slice of a hashref

am 15.11.2007 23:08:25 von glex_no-spam

David Filmer wrote:
> 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
};

Re: Syntax for a slice of a hashref

am 16.11.2007 00:44:20 von xhoster

David Filmer wrote:
> 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.

Re: Syntax for a slice of a hashref

am 16.11.2007 01:42:45 von Tad McClellan

David Filmer wrote:
> 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;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"