For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > Re: Slice returned hash or access to its elements without assigning









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Slice returned hash or access to its elements without assigning
Gunnar Hjalmarsson

2005-06-11, 3:57 pm

Andrew wrote:
> There is a sub foo which return hash:
>
> sub foo{my %in=( key1=>value1
> key2=>value2
> key3=>value3);
> }


Would that be the sub? Don't think so. It doesn't even compile, and even
if it had compiled, it would have returned a list rather than a hash.

Subroutines don't return hashes, but they may return references to hashes.

perldoc perlsub

> I need to assess elements of returned hash without assigning to a
> variable. Something like this:
> print @&foo(){'key1','key2'};
> print $&foo(){'key1'};


sub foo {
my %in = ( key1=>'value1', key2=>'value2', key3=>'value3' );
\%in
}

print @{ foo() }{'key1','key2'};

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com