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

Re: grep with looped tests

by LanX (Saint)
on Jun 06, 2014 at 15:31 UTC ( [id://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 ! :)

      update

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-18 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found