Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Grep Pattern

by LanX (Archbishop)
on Dec 12, 2018 at 16:43 UTC ( #1227155=note: print w/replies, xml ) Need Help??


in reply to Re^2: Grep Pattern
in thread Grep Pattern

> But you kinda messed up on the implementation.

Not really, you are filtering the sample input, hdb is filtering any input by a "repeating pattern".

update
DB<10> @pat = ( 0, 1, 1, 0 ); DB<11> x $i=0;grep { $pat[ $i++ % 4 ] } a..l; 0 'b' 1 'c' 2 'f' 3 'g' 4 'j' 5 'k' DB<12>

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^4: Grep Pattern
by hdb (Monsignor) on Dec 13, 2018 at 08:29 UTC

    Exactly. Like in the original question. "0..12" is an unfortunate choice for the sample input as $_ is always the same as $i in this case. Your test data illustrates better what really goes on.

Re^4: Grep Pattern
by ikegami (Pope) on Dec 13, 2018 at 18:25 UTC

    I stand by what I said. Even just hardcoding the 4 warranted horror. And you are mistaken about the restrictiveness of my code.

    @a[ grep { $pat[ $_ % @$pat ] } 0..$#a ]
    or
    sub filter_by_pat { my $pat = shift; return @_[ grep { $pat->[ $_ % @$pat ] } 0..$#_ ]; } filter_by_pat [0,1,1,0], ...
      > And you are mistaken about the restrictiveness of my code.

      Not really.

      There are many occasion where you need to handle a pure list in Perl and your array slice requires storing it in an array.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Let me rephrase: You are definitely mistaken about the restrictiveness of my code. The code I provided handles a pure list.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2019-11-21 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (105 votes). Check out past polls.

    Notices?