Now that is a truly interesting result, especially as when I modified it slightly to add my own variant I got a storm of undefined value used warnings from the chomp;s in the foreach loops that don't have anything to chomp on. When I fixed that I obtained the following results. (Note that I've not validated the operation of the individual functions)

Rate regex hash gf regex 1.94/s -- -73% -78% hash 7.17/s 269% -- -17% gf 8.67/s 347% 21% --
use strict; use warnings; use Time::HiRes; use Benchmark ':all'; my @data = (1000..9999); cmpthese (-2, { hash => 'hash()', gf => 'gf()', regex => 'regex()', }); sub regex { my @keep; foreach my $line (@data) { chomp $line; for ( split //, $line ) { my @a = ($line =~ m/$_/g); if (@a == 2) { push @keep, $line; last; } } } } sub hash { my @keep; # numbers to keep foreach my $line (@data) { chomp $line; my %count; ++$count{$_} for split '', $line; push @keep, $line if grep { $_ == 2 } values %count } } sub gf { my @keep = grep {my %d; map {$d{$_}++} split ''; grep {$_ == 2} valu +es %d} @data; }

DWIM is Perl's answer to Gödel

In reply to Re^5: Find duplicate digits by GrandFather
in thread Find duplicate digits by Anonymous Monk

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.