Kenny Madsen wrote:
>
> I define two arrays - one is an active changing file ( @unix = ps -ef ) and
> need to compare it to a static text file @static = `cat myfile`.
>
> Would someone help me on the syntax of greping for @static matches in the
> @unix array.
>
> For example
>
> #!/usr/bin/perl -w;
> use strict;
> my @unix = `ps -ef `;
> my @static = `cat myfile`;
> my @final = grep { @static} @unix;
> print @final; <==== I am wanting this to return only the files in myfile
> that match the @unix array.
>
> I am not sure if I need to assign a variable, for loop etc with in the { }
> to get the pattern matching results I need.
This is a Frequently Asked Question that is described in perlfaq4.pod.
perldoc -q "How do I compute the intersection of two arrays?"
Found in /usr/lib/perl5/5.6.0/pod/perlfaq4.pod
How do I compute the difference of two arrays? How do I
compute the intersection of two arrays?
Use a hash. Here's code to do both and more. It assumes
that each element is unique in a given array:
@union = @intersection = @difference = ();
%count = ();
foreach $element (@array1, @array2) { $count{$element}++ }
foreach $element (keys %count) {
push @union, $element;
push @{ $count{$element} > 1 ? \@intersection : \@difference },
$element;
}
Note that this is the symmetric difference, that is, all
elements in either A or in B, but not in both. Think of
it as an xor operation.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]