Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Syntax for a slice of a hashref

Reply
Thread Tools

Syntax for a slice of a hashref

 
 
David Filmer
Guest
Posts: n/a
 
      11-15-2007
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)
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      11-15-2007
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:umper;
print Dumper $hash_ref;


$VAR1 = {
'a' => 1,
'b' => 2
};
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-15-2007
David Filmer <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-16-2007
David Filmer <(E-Mail Removed)> 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/"
 
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
Combining statements - a hashref from a hash slice usenet@DavidFilmer.com Perl Misc 6 05-12-2006 06:05 PM
Formatting XML output from a hashref Sam Perl Misc 1 08-25-2005 08:42 AM
iterating over a hashref of hashrefs Sam Perl Misc 6 06-15-2005 10:44 AM
Equality of hashref objects bill Perl Misc 7 02-05-2005 07:54 PM
hashref strange side effects penguinista Perl Misc 3 12-24-2004 05:49 PM



Advertisments