Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: grep with looped tests

by LanX (Sage)
on Jun 06, 2014 at 15:31 UTC ( #1089045=note: print w/replies, xml ) Need Help??

in reply to grep with looped tests

I couldn't resist solving this with 2 nested grep! =)

DB<100> @x=1..10 => (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) DB<101> @re=qw/ 3 7 1 / => (3, 7, 1) DB<102> grep { my $data=$_; grep { $data =~ /$_/ } @re } @x => (1, 3, 7, 10)

Cheers Rolf

(addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: grep with looped tests
by RichardK (Parson) on Jun 06, 2014 at 15:57 UTC

    Similarly, using first from List::Util to short circuit the inner loop when you match a line :)

    use v5.18; use warnings; use List::Util 'first'; use Data::Dumper; my @data = 0..10; my @re = (3,7,9); my @res = grep {my $line = $_; !defined first {$line =~ $_} @re } @dat +a; say Dumper(\@res);
      Ah I always forget that List::Util (which is core) has now any , too!

      (I'm still hooked on List::MoreUtils , see next answer :)

      > my @res = grep {my $line = $_; !defined first {$line =~ $_} @re } @data;

      But I think you'd certainly prefer any over first to avoid !defined ! :)


      though first was and any wasn't part of my 5.10 distribution!

      Cheers Rolf

      (addicted to the Perl Programming Language)

Re^2: grep with looped tests
by Laurent_R (Canon) on Jun 06, 2014 at 20:55 UTC
    This is a quite nice construct. I had a somewhat similar problem to solve a couple of weeks ago and did not think about such a clever and concise solution. Having said that, the explicit loop I used was probably not less efficient. But I wish I had thought about such a solution as yours.

    Rolf, if I may, a brief side question: which option of the debugger are you using for displaying directly last evaluated expression without having to print is explicitly? I mean, when you have this:

    DB<100> @x=1..10 => (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    what option do you use to have the second line above printed seemingly automatically? Thanks for your response, and sorry for being off-topic.

      Unfortunately there is no option, I patched the code. (after a hint here from pemungkah )

      Actually monkey patched some lines from a config file.

      I can share later if you want, I'm mobile ATM.

      Cheers Rolf

      (addicted to the Perl Programming Language)

        Thanks for your answer, Rolf. I am not sure I want to patch Perl, I don't want to break everything, unless it is a simple thing that can be easily undone.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2022-05-18 14:18 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.