in reply to comparing two arrays and displaying scalar matches

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; } }

Replies are listed 'Best First'.
Re^2: comparing two arrays and displaying scalar matches
by kennethk (Abbot) on Nov 30, 2010 at 21:43 UTC
    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.