removing selected lines

S

Shiraz

(i have a file with the format below:
AA,BB,CC
AA,BB,FF
AA,BB,RF
AA,QQ,VV

and after processing i need to have in it

AA,QQ,WW

throw out all the duplicates and leave none in the system

bwlow is my code: please help!!!!!!!!!!




open (FH, "<rates/temp2.csv") or die ("No input file\n");
($memLine1, $memLine2, $memLine3, $lineCount)=("","","",0);
$lineCount = 1;
while ($record11 = <FH>)
{
$lineCount=$lineCount+1;
if ($lineCount == "1") {$memLine1 = $record11; print
("xxxxxx1xxxxxx");}
if ($lineCount == "2") {$memLine2 = $record11; print
("xxxxxx2xxxxxx");}
if ($lineCount == "3") {$memLine3 = $record11; print
("xxxxxx3xxxxxx");}
if ($lineCount == 3)
{
if ( ($memLine2 != $memLine1) || ($memLine2 != $memLine3))
{
print ("Debug1 $memLine1\n Debug2 $memLin2\n Debug3 $memLine3
\n");
# print ("$memLine2");
$memLine1 = $memLine2;
$memLine2 = $memLine3;
}
$lineCount = 2;
}
}
 
A

Arndt Jonasson

Shiraz said:
(i have a file with the format below:
AA,BB,CC
AA,BB,FF
AA,BB,RF
AA,QQ,VV

and after processing i need to have in it

AA,QQ,WW

throw out all the duplicates and leave none in the system

I don't understand your specification. Where did the "WW" come from?
And even if that's supposed to be "AA,QQ,VV", why is "AA,BB,CC" removed?
 
J

John W. Krahn

Shiraz said:
(i have a file with the format below:
AA,BB,CC
AA,BB,FF
AA,BB,RF
AA,QQ,VV

and after processing i need to have in it

AA,QQ,WW

throw out all the duplicates and leave none in the system

bwlow is my code: please help!!!!!!!!!!

use warnings;
use strict;
open (FH, "<rates/temp2.csv") or die ("No input file\n");

You should include the $! variable in the error message so you know why it died.
($memLine1, $memLine2, $memLine3, $lineCount)=("","","",0);
$lineCount = 1;

Why the two different assignments to $lineCount?
while ($record11 = <FH>)
{
$lineCount=$lineCount+1;

You are adding 1 to $lineCount so it starts out with a value of 2.
if ($lineCount == "1") {$memLine1 = $record11; print
("xxxxxx1xxxxxx");}

Since $lineCount never has a value of 1 that will never execute.
if ($lineCount == "2") {$memLine2 = $record11; print
("xxxxxx2xxxxxx");}
if ($lineCount == "3") {$memLine3 = $record11; print
("xxxxxx3xxxxxx");}

You compare numbers with == but "1", "2" and "3" are strings which means that
perl has to convert the strings to numbers. Just use numbers (like the next
line does.)
if ($lineCount == 3)
{
if ( ($memLine2 != $memLine1) || ($memLine2 != $memLine3))

You compare strings with ne instead of !=. Comparing strings as numbers will
not work. Your logic implies that $memLine1 and $memLine3 can be the same but
neither can match $memLine2. Is that what you intended?
{
print ("Debug1 $memLine1\n Debug2 $memLin2\n Debug3 $memLine3
\n");
# print ("$memLine2");
$memLine1 = $memLine2;
$memLine2 = $memLine3;
}
$lineCount = 2;
}
}

A more perlish way to write that would be (untested):

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

open FH, '<', 'rates/temp2.csv' or die "Cannot open 'rates/temp2.csv' $!";

my @memLine;

while ( my $record = <FH> )
{
push( @memLine, $record ) <= 3 and next;

if ( $memLine[ 1 ] ne $memLine[ 0 ] or $memLine[ 1 ] ne $memLine[ 2 ] )
{
print 'Debug', $_ + 1, " $memLine[$_]\n" for 0 .. $#memLine;
}

shift @memLine;
}

__END__



John
 
J

Jürgen Exner

Shiraz said:
(i have a file with the format below:
AA,BB,CC

A namesake of yours just posted exactly the same question in the thread
"delete based on duplicate field".
Mabye you can ask him to forward his responses to you.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top