removing selected lines

Discussion in 'Perl Misc' started by Shiraz, Jun 16, 2005.

  1. Shiraz

    Shiraz Guest

    (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;
    }
    }
     
    Shiraz, Jun 16, 2005
    #1
    1. Advertising

  2. "Shiraz" <> writes:
    > (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?
     
    Arndt Jonasson, Jun 16, 2005
    #2
    1. Advertising

  3. Shiraz wrote:
    > (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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 16, 2005
    #3
  4. Shiraz wrote:
    > (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
     
    Jürgen Exner, Jun 16, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Taylor
    Replies:
    2
    Views:
    1,113
    Eric J. Roode
    Aug 4, 2003
  2. Ned D Hanks
    Replies:
    0
    Views:
    932
    Ned D Hanks
    Aug 4, 2003
  3. Replies:
    0
    Views:
    475
  4. Delali Dzirasa

    Removing Selected rows in JTable????

    Delali Dzirasa, Jan 19, 2004, in forum: Java
    Replies:
    2
    Views:
    28,438
    ekowus
    Aug 11, 2007
  5. Chris  Chiasson
    Replies:
    6
    Views:
    638
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page