Sent from my LG phone "John W. Krahn" <jwkr...@shaw.ca> wrote:
>Chris Stinemetz wrote: >> 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/. > >AND means matching BOTH in the same line, but you only want to match ONE >of the array elements so that should be "rcsm OR cdmno". > > >> 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 ) { > >$i now contains either 'rcsm' or 'cdmno'. > > >> print $FHOUT $_ =~ s/$keyFields[$i]\d/match/; > >Using a string as an array index will convert that string to a numeric >value, in this case the number 0, so $keyFields[$i] will always evaluate >to 'rcsm' as that is the first element of @keyFields. > >The substitution operator will return either 1 or '' (true or false) >which is probably not what you want. > > >> } >> } >> >> __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 > > >$ echo "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" | perl -e' > >my @keyFields = qw/ rcsm cdmno /; > >while ( my $line = <> ) { > print $line unless grep $line =~ /$_\d/, @keyFields; > } >' >cdmno=1 >rdnt_cdmno=1 >cdmno=2 >rcsm=801 >rcsm=801 >rcsm=802 > > > > >John >-- >Any intelligent fool can make things bigger and >more complex... It takes a touch of genius - >and a lot of courage to move in the opposite >direction. -- Albert Einstein > >-- >To unsubscribe, e-mail: beginners-unsubscr...@perl.org >For additional commands, e-mail: beginners-h...@perl.org >http://learn.perl.org/ > > >