Using Regex/Grep to grab lines from an array using an array of patterns?

Inside of grep $ refers to the list element involved in the test. Also abc is short for $_ =~ /abc so you're effectively testing $_ =~ /$ guess what the answer is likely to be (with no metacharacters)? So you're passing all values into regression_links What you need to do is save the value of $ But since you're not using the simple print statement, you could just as easily reserve the $ variable for the grep, like so: foreach my $reg ( @regressions ) { print "$reg\n"; @regression_links = grep(/$reg/, @current_test_summary_file ); } However, you're resetting regression_links with each loop, and a push would work better: push @regression_links, grep(/$reg/, @current_test_summary_file ) However, a for loop is a bad choice for his anyway, because you could get duplicates and you might not want them.

Since you're matching by regex, one alternative with multiple criteria is to build a regex alternation. But in order to get a proper alternation, we need to sort it by length of string descending and then by alphabetic order ( cmp ) create the alternation expression my $filter = join( '|' , sort { length( $b ) length( $a ) || $a cmp $b } @regressions ); @regression_links = grep( /$filter/, @current_test_summary_file ) Or other than concatenating a regex, if you wanted to test them separately, the better way would be with something like List::MoreUtils::any : regression_links = grep { my $c = $_; # save $_ return any { /$c/ } @regressions; } @current_test_summary_file.

Inside of grep, $_ refers to the list element involved in the test. Also /abc/ is short for $_ =~ /abc/ so you're effectively testing $_ =~ /$_/ guess what the answer is likely to be (with no metacharacters)? So you're passing all values into @regression_links.

What you need to do is save the value of $_. But since you're not using the simple print statement, you could just as easily reserve the $_ variable for the grep, like so: foreach my $reg ( @regressions ) { print "$reg\n"; @regression_links = grep(/$reg/, @current_test_summary_file ); } However, you're resetting @regression_links with each loop, and a push would work better: push @regression_links, grep(/$reg/, @current_test_summary_file ); However, a for loop is a bad choice for his anyway, because you could get duplicates and you might not want them. Since you're matching by regex, one alternative with multiple criteria is to build a regex alternation.

But in order to get a proper alternation, we need to sort it by length of string descending and then by alphabetic order (cmp). # create the alternation expression my $filter = join( '|' , sort { length( $b ) length( $a ) || $a cmp $b } @regressions ); @regression_links = grep( /$filter/, @current_test_summary_file ); Or other than concatenating a regex, if you wanted to test them separately, the better way would be with something like List::MoreUtils::any: @regression_links = grep { my $c = $_; # save $_ return any { /$c/ } @regressions; } @current_test_summary_file.

Second idea worked thanks guess you meant push(@regression_link, grep(/$regression/, @current_test_summary_file));. Quick question can you explain why the first example only displays the middle result (table)? – Ion Jul 26 at 15:07 Any chance you can explain what the other two examples are doing as damm hard to read them (well for me as a noob :) )?

– Ion Jul 26 at 15:18 It wouldn't hurt to map { +quotemeta } before joining. – Zaid Jul 26 at 17:31.

Axeman is correct and localising $_ with $reg will solve your problem. But as for pulling out matches I would naively push all matches onto @regression_links producing a list of (probably) multiple matches. You can then use List::MoreUtils::uniq to trim down the list.

If you don't have List::MoreUtils installed you can just copy the function (its 2 lines of code). # Axeman's changes foreach my $reg (@regressions) { print "regression: $reg\n"; # Push all matches. Push @regression_links, grep(/$reg/, @current_test_summary_file); } # Trim down the list once matching is done.

Use List::MoreUtils qw/uniq/; foreach ( uniq(@regression_links) ) { print "$_\n"; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions