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

Re: RFC 201 (v1) hash slicing

From:
Bart Lateur
Date:
September 9, 2000 10:07
Subject:
Re: RFC 201 (v1) hash slicing
Message ID:
j4rkrs8jp16ogncmb6f6ispehkf5a8u4k7@4ax.com
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.



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