Re: comparing two arrays and displaying scalar matches
by kennethk (Abbot) on Nov 30, 2010 at 16:10 UTC
|
@array = <FILE>;
@pattern = <FILE2>;
local $" = '|';
foreach $line(@array) {
if ($line =~ m/@pattern/) {
print $line;
}
}
| functions as an or in regular expressions; see Metacharacters in perlre. If your input may contain regular expression metacharacters, you can use \Q and \E with a map to escape those and accomplish the same thing (see Quote and Quote like Operators in perlop):
@array = <FILE>;
@pattern = map "\Q$_\E", <FILE2>;
local $" = '|';
foreach $line(@array) {
if ($line =~ m/@pattern/) {
print $line;
}
}
Finally, please wrap code in <code> tags to preserve formatting and make downloading code easier for monk who wish to help; see Writeup Formatting Tips. | [reply] [d/l] [select] |
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.
- For every pattern,
- For every line,
- If the line matches the pattern,
- 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.
- my $pattern = join '|', @patterns;
- For every line,
- If the line matches the combined pattern,
- Print the line.
| [reply] [d/l] |
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);
| [reply] [d/l] |
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! | [reply] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
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;
}
}
| [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
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>
}
| [reply] [d/l] |
|
|
Ideas:
- 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.
- Proofread, test anything you plan to post here with perl -c scriptname.
- 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.
| [reply] [d/l] [select] |