I'll offer yet another solution....
There is a requirement that each of the letters much appear at least once. And I interpret "order" to mean the order that they appeared in the string, not in frequency or order of the list that we are looking for...maybe that is wrong.

The match global will just go through the input string once. The while() loop iterates over a list that the match global expression produced. I'm not sure how "smart" regex is with /o option (compile regex once). Having a join may be slower than I think, but my testing so far with Perl 5.10 says that regex is way smarter (and faster) than it used to be! You can do the join other ways or declare other variables. One main point about regex is that picking from a set [xyz] is a lot faster than /x/|/y/|/z|.

Map and sort are pretty "heavy weight" critters. A hash table especially with a small number of keys is very efficient. A Perl hash gets created by default with 8 hash "buckets". Don't be afraid to use a hash. This code is verbose, but is straightforward and will run quickly.

#!/usr/bin/perl -w use strict; my $str = "1b22a3d3cab"; my @letters = qw (a b 2 c d); my @order_found =(); my %seen =(); #UPDATE: don't need the $found variable, oops! #while (my $found = $str =~ m/([join("",@letters)])/go) #should have been: while ($str =~ m/([join("",@letters)])/go) { push (@order_found, $1) unless $seen{$1}; $seen{$1}++; } if (keys %seen != @letters) { print "not all letters found\n"; } else { print @order_found, "\n"; # prints b2adc }

In reply to Re: How to replace greedy alternation? by Marshall
in thread How to replace greedy alternation? by adamcrussell

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.