For efficiency, you generally want to avoid map expressions that return more than one element for each element of the original list, at least in current versions of Perl. As I understand it, map preallocates a list to hold the results, but when you return several elements for each individual element, there's a lot of overhead to accomodate the extra elements.

Here's a way to do the transfer without using map:

my @keys = grep /^t/, keys %hash; my %newhash; @newhash{@keys} = @hash{@keys};
On the other hand, I did a quick benchmark, which ended up showing little difference in efficiency between the solutions:
#!perl -w use strict; use Benchmark; Benchmark->import(qw/cmpthese/) if $^V; my $time = shift || 5; my $size = shift || 100; my %hash; for (0 .. $size) { if ($_ < $size / 4) { $hash{"t$_"} = 1; } else { $hash{"a$_"} = 1; } } my %bms = ( grep_map => sub { my @keys = grep /^\t/, keys %hash; my %newhash = map { ($_, $hash{$_}) } +@keys; }, just_map => sub { my %newhash = map /^\t/ ? ( $_, $hash{$_} ) : (), keys %hash; }, grep_slice => sub { my @keys = grep /^\t/, keys %hash; my %newhash; @newhash{@keys} = @hash{@keys}; }, ); if ($^V) { cmpthese(-$time, \%bms); } else { timethese(-$time, \%bms); } __END__ % perl bm.pl Benchmark: running grep_map, grep_slice, just_map, each for at least 5 + CPU seconds... grep_map: 6 wallclock secs ( 5.29 usr + 0.01 sys = 5.30 CPU) @ 40 +34.15/s (n=21381) grep_slice: 6 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 40 +47.73/s (n=21372) just_map: 6 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 39 +90.06/s (n=20868) Rate just_map grep_map grep_slice just_map 3990/s -- -1% -1% grep_map 4034/s 1% -- -0% % perl/bin/perl bm.pl 5 1000 Benchmark: running grep_map, grep_slice, just_map, each for at least 5 + CPU seconds... grep_map: 6 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 41 +2.17/s (n=2168) grep_slice: 6 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 41 +2.95/s (n=2168) just_map: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 39 +8.87/s (n=2110) Rate just_map grep_map grep_slice just_map 399/s -- -3% -3% grep_map 412/s 3% -- -0% grep_slice 413/s 4% 0% --
So I guess you can just go with whichever solution you like best. :)

In reply to Re: Miniature golf question by chipmunk
in thread Miniature golf question by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.