Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I don't think your solution is that bad. If you want to get rid of the two reverse, I think you have to buy and additional list function instead, for instance:
List::MoreUtils::after_incl { $_ !~ /DBIC/ } List::MoreUtils::after_incl { /DBIC/ } List::MoreUtils::before { /Dancer/ } @YourList
I consider this even more ugly. Alternatively you could try
my $partition=0; my @parts = List::MoreUtils::part { if($pivots[0]) { if($partition % 2 == 0) { if($_ =~ $pivots[0]) { ++$partition; } } else { if($_ !~ $pivots[0]) { ++$partition; shift @pivots; } } } $partition } qw<eval DBIC::3 DBIC::2 DBIC::1 MyApp::3 MyApp::2 MyApp::1 Dancer::3 + Dancer::2 Dancer::1>; print join(',',@{ $parts[2] }),"\n";
Whether this is more readable, is something to be questioned, but at least the code can be generalized easier to more than 2 "pattern changes". However, it silently assumes that part traverses the list left to right, which is not guaranteed by the documentation.

Another solution (which does not use reverse) would be

my @input=qw<eval DBIC::3 DBIC::2 DBIC::1 MyApp::3 MyApp::2 MyApp::1 D +ancer::3 Dancer::2 Dancer::1>; my @firsts=@input[0..@input-2]; my @seconds=@input[1..@input-1]; my $inside=0; my @result; List::MoreUtils::pairwise { if($inside == 1) { my $pat=qr(Dancer); push @result,$a; if($a !~ $pat && $b =~ $pat) { $inside=-1; } } elsif($inside == 0) { my $pat=qr(DBIC); if($a =~ $pat && $b !~ $pat) { $inside=1; } } } @firsts, @seconds; print("@result\n");
but this is forcing the pairwise function to something in a way it was not really meant to be used (since the result of pairwise is never used).
Maybe you are better off writing a conventional loop and storing the state (inside / outside your pattern sequence) in a status variable, similar to my part example, but without (mis-)using List::MoreUtils.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Re: List::MoreUtils before, after and ... between? by rovf
in thread List::MoreUtils before, after and ... between? by Boldra

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2014-07-22 14:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (115 votes), past polls