pipeops has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have two arrays and I am trying to find if one of them
contains a word from the other one array.
This is pretty much a blind search from two files and
values change daily. I know what I have below is incorrect
and was wondering how I can correct that:

@array = <FILE>; @pattern = <FILE2>; foreach $line(@array) { if ($line =~ m/@pattern/) } print $line; } }

Replies are listed 'Best First'.
Re: comparing two arrays and displaying scalar matches
by kennethk (Abbot) on Nov 30, 2010 at 16:10 UTC
Re: comparing two arrays and displaying scalar matches
by ikegami (Patriarch) on Nov 30, 2010 at 16:00 UTC

    You want to check all patterns against all lines. That's two loops.

    1. For every pattern,
      1. For every line,
        1. If the line matches the pattern,
          1. Print the line.

    To avoid printing the same line more than once, you could join the patterns into one. That moves one of the loops into the regex engine.

    1. my $pattern = join '|', @patterns;
    2. For every line,
      1. If the line matches the combined pattern,
        1. Print the line.
Re: comparing two arrays and displaying scalar matches
by pipeops (Novice) on Dec 01, 2010 at 15:52 UTC
    Thanks everyone for help. I used the following lines. I'm a beginner at perl and did read the Perl book, but without jumping into the code and experiencing the problems and fixing them, there is no way to better learn :)
    foreach $line (@array) { foreach $pat (@pattern) { if($line =~ /$pat/) { push (@new_array, $line);
Re: comparing two arrays and displaying scalar matches
by pipeops (Novice) on Nov 30, 2010 at 16:29 UTC
    Thanks for that...however my @array contains records like:
    @array = ("TEST SUCCEEDED OK | 11", "TEST FAILED ABORT | 24", "WORDONE", "SCENARIO 2");
    So lets say @pattern = ("TEST", "SCENARIO");
    I will need the final output to say:
    TEST SUCCEEDED OK | 11
    SCENARIO 2
    Any ideas?
    Thanks!
      TEST FAILED ABORT | 24 also contains the pattern TEST.

      Why shouldn't TEST FAILED ABORT | 24 be in the output as well?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: comparing two arrays and displaying scalar matches
by pipeops (Novice) on Nov 30, 2010 at 21:06 UTC
    Can I do something something similar like this...this still doesnt work, but I feel like I am close to the solution here...
    $size = @array; $size2 = @pattern; foreach $line(@array) { if (@array[0 .. $size] =~ @pattern[0 .. $size2]) { print $line; } }
      What background do you have in programming? I mean this with respect - it seems like you are currently trying to tackle a problem which is beyond your current understanding. For example,
      • You assign $line as an iterator, but then you never use the value. See Foreach Loops.
      • =~ (Binding Operators) is a scalar operator, but you are feeding it two array Slices.
      • The upper index you are using on your array slices is larger than the biggest index in the array.

      We are glad to help neophytes learn the language, and will do our best to guide your development. I would strongly suggest you either take a course or get a good reference book, such as Programming Perl, Third Edition.

      While some significant optimizations have been suggested above, the simplest code which seems to conform to what you expect might look something like:

      foreach $line (@array) { foreach $pat (@pattern) { if ($line =~ m/$pat/) { print $line; } } }

      This might not behave as you expect, depending on what is in your @patterns array - see perlretut for some introductory material on regular expressions.

Re: comparing two arrays and displaying scalar matches
by pipeops (Novice) on Nov 30, 2010 at 17:19 UTC
    Unfortunately, this doe not seem to work any other ideas?

    @array = <FILE>;<br> @pattern = <FILE2>;<br> foreach $line(@array)<br> {<br> if ($line =~ m/@pattern/)<br> }<br> print $line;<br> }<br> }
      Ideas:
      1. use strict; use warnings; would have told you that the curly at line 6 faces the wrong way (is a close_curly, not the open required in an if... clause.
         
      2. Proofread, test anything you plan to post here with perl -c scriptname.
         
      3. Review the use of <c>...</c> (or <code>...</code>) tags at Markup in the Monastery. The HTML break tags don't belong inside code tags unless your code actually has <br> tags.