develooper Front page | | Postings from September 2000

Re: RFC 201 (v1) hash slicing

Bart Lateur
September 9, 2000 10:07
Re: RFC 201 (v1) hash slicing
Message ID:
On 8 Sep 2000 04:57:46 -0000, Perl6 RFC Librarian wrote:

>Instead of
>	%subhash = map { f($_) ? ($_, $hash{$_}) : () } keys %hash;	# lengthy
>one may now write
>	%subhash = %hash{f($_)};	# code block f($_) will be evaluated for Truth over all the keys

I almost liked that one.

I like what it produces: a hash slice that contains not only the values,
but the keys and the values, interleaved.

But I do not like the built-in grep.

I would prefer this syntax:

	%extract = %hash{@keys};

which is like:

	%extract = map { $_ => $hash{$_} } @keys;

It's more in sync with current perl:

	@values = @hash{@keys};

which returns a list of (just the) hash values.

If you want the grep, you can use it explicitely:

	%extract = %hash{ grep f($_), keys %hash };

	Bart. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About