Johnson, Reginald (GTI) wrote:
In my code I am using grep successfully, but I would also like an
output file that has the objects that don't match the grep. I am trying
to capture the $line of my <INFILE>  that don't match.
I am thinking something like if ([EMAIL PROTECTED] =  
grep/\b$line\b/i,@mhsArray)
{  }
#!/usr/bin/perl
use warnings;

        $file="/adsm/CRONJOBS/MHS/nodes_cleanedup";
        $mhs="/adsm/CRONJOBS/MHS/mh_alloc.csv";
        $file_out="/adsm/CRONJOBS/MHS/in_mhs";

        open (INFILE, "<", "$file") or
                        die "$file could not be opened: $!";
        open (MHSFILE, "<", "$mhs") or
                        die "$mhs could not be opened: $!";
        open (OUTFILE, ">", "$file_out") or
                        die "$file_out could not be opened: $!";

        while (<MHSFILE>) {
                chomp($_);
                push (@mhsArray, $_);
                }
        while ($line= <INFILE>) {
                chomp($line);
                print "$line\n";
                @inmhs =  grep/\b$line\b/i,@mhsArray;
          foreach $line (@inmhs) {
                print OUTFILE "$line\n";
            }
        } #end while
close(INFILE);
close(MHSFILE);

Instead if using grep() just use a foreach loop:

#!/usr/bin/perl
use warnings;
use strict;

my $file     = '/adsm/CRONJOBS/MHS/nodes_cleanedup';
my $mhs      = '/adsm/CRONJOBS/MHS/mh_alloc.csv';
my $file_out = '/adsm/CRONJOBS/MHS/in_mhs';

open MHSFILE, '<', $mhs or die "$mhs could not be opened: $!";
chomp( my @mhsArray = <MHSFILE> );
close MHSFILE;

open INFILE,  '<', $file     or die "$file could not be opened: $!";
open OUTFILE, '>', $file_out or die "$file_out could not be opened: $!";

while ( my $line = <INFILE> ) {
    chomp $line;
    print "$line\n";
    for my $new_line ( @mhsArray ) {
        if ( $new_line =~ /\b$line\b/i ) {
            print OUTFILE "$new_line\n";
            }
        else {
            print "$new_line\n";
            }
        }
    }

close INFILE;
close OUTFILE;

__END__


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to