I am trying ot find a way to use an array as a reference to remove
lines from a file.
The array @keyFields has the elements "rcsm and cdmno". My objective
is to remove any line from the input that matches the regex /rcsm\d/
and cdmno\d/.

I'm not sure if I'm using the best approach.

I am getting the following error:

Argument "rcsm" isn't numeric in array element at deleteData1.pl line
15, <DATA> line 1.
Argument "cdmno" isn't numeric in array element at deleteData1.pl line
15, <DATA> line 1.

#!/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 ( @keyFields ) {
    print $FHOUT $_ =~ s/$keyFields[$i]\d/match/;
  }
}

__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

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to