[Israel.pm] Case Study: Splitting a Hash into Two

Yuval Yaari yuval at windax.com
Thu Jul 15 12:15:05 PDT 2004


Funny, that's exactly what I had in mind when reading the original post.
I was wondering if someone isn't as lazy as I am and actually benchmarked
it???

I just wanted to say that this kind of programming gives Perl a bad name
[in my eyes, at least].

And even though a map {} grep chain is ugly/hack-ish -- perldoc -f on each
of those would give you the right picture.
The only things I can perldoc out of your code are "while" and "each" and
they wouldn't help a user get a good understanding at what your code does.

I would add that I might be biased because `map` must be my all-time
favorite Perl built-in function.

  --Yuval


Mikhael Goikhman said:
> On 14 Jul 2004 21:48:30 +0300, Shlomi Fish wrote:
>>
>> Today I hanged around FreeNode's #perl channel. Someon asked for a way
>> to  generate two hashes out of one, with one containing only the keys
>> that start  with underscore, and the other the rest. After some
>> thinking and planning I  came out with this code:
>>
>> <<<
>> sub split_hash
>> {
>>     my $h = shift;
>>     # us is underscore not the United States.
>>     my (%us_hash, %non_us_hash);
>>     while (my ($k, $v) = each(%$h))
>>     {
>>         (($k =~ /^_/o) ? \%us_hash : \%non_us_hash)->{$k} = $v;
>>     }
>>     return (\%us_hash, \%non_us_hash);
>> }
>> >>>
>>
>> (tested, BTW).
>
> I think I would do it this way (at least if the run time is cheap):
>
> 	my %under_hash = map { $_ => $hash{$_} } grep  /^_/, keys %hash;
> 	my %other_hash = map { $_ => $hash{$_} } grep !/^_/, keys %hash;
>
> Regards,
> Mikhael.
>
> --
> perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl






More information about the Perl mailing list