develooper Front page | perl.perl6.language.data | Postings from September 2000

Re: RFC 196 (v1) More direct syntax for hashes

Thread Previous
From:
Bart Lateur
Date:
September 7, 2000 08:10
Subject:
Re: RFC 196 (v1) More direct syntax for hashes
Message ID:
1jbfrsshnb73al4h3bg980b0bm70saro7i@4ax.com
On Wed, 6 Sep 2000 22:58:05 -0400, John Porter wrote:

>	keys %hash = @things;
>
>is defined as being equivalent to
>
>	@hash{ @things } = ();
>
>This is to support hash-based set operations in a more
>natural way, i.e.
>
>	keys %hash = grep { exists $a{$_} } keys %b;

I have doubts if this would indeed make life simpler. It's a shortcut
for something that is already quite simple, but doing nothing for
slightly more complex situations. In short:

	@hash{@keys} = ();

is the simple case. It can work even if  @keysis not an array, but a
list instead (a grep() expression, for example). These are more complex
but related things:

	@hash{@keys) = 0 .. $#keys;

Try *that* with a list.

Or, coming back to your example: fill a new hash with key/value pairs of
%b with only where intries in %a exist:

	@hash{grep { exists $a{$_} } keys %b} = 
	   @b{grep { exists $a{$_} } keys %b};

Ugh.

If you can find a simple alternative syntax for these two, then you have
my blessing.

-- 
	Bart.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About