> -----Original Message-----
> From: Chris Stinemetz [mailto:[email protected]]
> Sent: Monday, February 20, 2012 3:51 PM
> To: [email protected]
> Subject: Re: search and replace with an array
>
> Looks like I was able to figure it out with the below:
>
> But how do I remove all the blank lines?
>
> Output:
>
> cdmno=1
> rdnt_cdmno=1
>
...
>
> cdmno=2
> rcsm=801
>
> rcsm=801
>
...
>
> rcsm=802
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $file = "nonKeys.txt";
> my $newFile = "cleanKeys.txt";
> my @keyFields = qw(rcsm cdmno);
>
> #open my $FH, '<', $file or die "ERROR opening $file: !"; open my
> $FHOUT, '>', $newFile or die "ERROR opening $newFile: !";
>
> while ( <DATA> ) {
> chomp;
> for my $i ( 0 .. $#keyFields ) {
> $_ =~ s/$keyFields[$i]\d.*//;
> }
> print "$_\n";
> }
> __DATA__
> cdmno=1
> rdnt_cdmno=1
> cdmno2=1
> cdmno3=1
> cdmno4=1
> cdmno5=1
> cdmno6=1
> cdmno7=1
> cdmno8=1
> cdmno=2
> rcsm=801
> rcsm2=801
> rcsm3=801
> rcsm4=801
> rcsm5=801
> rcsm6=801
> rcsm7=801
> rcsm8=801
> rcsm=801
> rcsm2=801
> rcsm3=801
> rcsm4=801
> rcsm5=801
> rcsm6=801
> rcsm7=801
> rcsm8=801
> rcsm=802
>
Hi Chris,
for my $i ( 0 .. $#keyFields ) {
$_ =~ s/$keyFields[$i]\d.*//;
}
Would usually be written as:
for my $keyField ( @keyFields ) {
$_ =~ s/$keyField\d.*//;
}
Based on the fact that you are creating blank lines with your substitute
command, and now you wish to eliminate them - the substitution appears to
not be needed.
Therefore, just go to the next line of input if you have a match. For
instance:
DATA_LOOP:
while ( <DATA> ) {
for my $keyField ( @keyFields ) {
next DATA_LOOP if /$keyField\d.*/;
}
print;
}
Notice I removed the chomp as it is not necessary in this case.
HTH, Ken
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/