I would also use matching instead of substitution. You mentioned in a private message that the substitution is faster (using such a trick would be worth a comment), but my benchmark doesn't show the effect. To speed things up, rather use my %h instead of %h = ().
#!/usr/bin/perl use warnings; use strict; use Test::More; use Benchmark qw{ cmpthese }; my @input = split /\n/, << '__INPUT__'; 12-12 12-12-12 12-13-12-13 12-12-13-13 12-13-13-14 __INPUT__ sub lenno { my @back; my @i = @input; for (@i) { my %h; undef $h{$1} while s/(\d\d)//; push @back, join ("-", sort keys %h); } \@back } sub choro { my @back; my @i = @input; for (@i) { my %h; undef $h{$1} while m/(\d\d)/g; push @back, join ('-', sort keys %h); } \@back } is_deeply(lenno(), choro(), 'same'); done_testing(); cmpthese(-3, { lenno => 'lenno', choro => 'choro', });
Output:
Rate lenno choro lenno 25358/s -- -4% choro 26458/s 4% --

Perl 5.16.2, x86_64-linux-thread-multi.

Update: Significantly faster: use a hash slice undef @h{m/\d\d/g}; instead of the inner loop.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re^2: How can I reduce this with a REGEX? by choroba
in thread How can I reduce this with a REGEX? by misterperl

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.