Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: List::MoreUtils before, after and ... between?

by rovf (Priest)
on Feb 21, 2012 at 15:53 UTC ( #955338=note: print w/ replies, xml ) Need Help??


in reply to List::MoreUtils before, after and ... between?

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>


Comment on Re: List::MoreUtils before, after and ... between?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-27 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls