Re: lvalue hash slice

"John W. Krahn" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
Jayaprakash Rudraraju wrote:
> 
> 1:32pm, IP packets from [email protected] delivered:
> 
> > On Tue, Sep 30, 2003 at 11:07:24PM +0300, Gaal Yahas wrote:
> > > I like hash slices:
> > >
> > >     ($one, $two, $three) = @hash{ qw/aaa bbb ccc/ };
> > >
> > > Sadly, this doesn't work as an lvalue:
> > >
> > >     @hash{ qw/aaa bbb ccc/ } = ($one, $two, $three);        # WRONG
> >
> > It doesn't?
> >
> > $ perl -wle '$one=1; $two=2; $three=3; @h{qw(a b c)} = ($one,$two,$three);  print %h'
> > c3a1b2
> 
> You cannot print hash, like you can print a list.

A hash in list context returns a list, so yes, you can.

$ perl -le'
$, = $";
%h = ( a => 1, b => 2, c => 3, d => 4 );
print %h;
++$_ for %h;
print %h;
'
a 1 b 2 c 3 d 4
a 2 b 3 c 4 d 5


> Because elements in the
> hash are stored differently to optimize retrieval of hash elements.

That just means that they won't be in the same order that you put them
there.


John
-- 
use Perl;
program
fulfillment
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.