Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Grep Pattern

by Aldebaran (Chaplain)
on Dec 15, 2018 at 00:38 UTC ( #1227285=note: print w/replies, xml ) Need Help??


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

that's elegant...

$ ./1.grep_pattern.pl result is 1 2 5 6 9 10 $ cat 1.grep_pattern.pl #!/usr/bin/perl -w use 5.011; my @pat = ( 0, 1, 1, 0 ); my @result = grep { $pat[ $_ % @pat ] } 0..12; say "result is @result"; __END__ $

Replies are listed 'Best First'.
Re^4: Grep Pattern
by LanX (Archbishop) on Dec 15, 2018 at 01:36 UTC
    > that's elegant..

    One should even be able to shorten it to (can't test right now)

     grep { (0,1,1,0)[ $_ % 4 ] } 0..12;

    But it's not as DRY, cause you need to change the 4 if you change the list.

    And of course it's not answering the OP s real question to filter any list.

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

      I was about to say it also creates a shitload of scalars, but it doesn't. `0,1,1,0` repeatedly places the same 4 scalars on the stack. They are created at compile-time and associated with the opcode that returns them. Not bad

Log In?
Username:
Password:

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

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



    Results (95 votes). Check out past polls.

    Notices?