Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Perl delete function

by annel (Novice)
on Dec 23, 2013 at 05:52 UTC ( #1068138=perlquestion: print w/replies, xml ) Need Help??

annel has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, let's say if
text1: apple orange text2: apple apple
I try to delete the keys which are common in both text (only match once) and print out the mismatch. Apple of line 1 from text 1 and apple of line 1 from text 2 match, therefore deleted. But somehow apples of both lines in text 2 were deleted. Is there any better ways to delete just once for each existed data? After all of your generous help and tips,here is the code I have written. Cheers.
use strict; use warnings; open (FILE, "a") or die "Unable to open ref file.\n"; chomp (my @data1 =<FILE>) ; close(FILE); open (FILE, "b") or die "Unable to open new file.\n"; chomp (my @data2 =<FILE> ); close(FILE); my $size = @data1 < @data2 ? @data1 : @data2; my $result= @data1 < @data2 ? "File 1 has missing data" : "File 2 has +missing data"; my $ori = @data1 == @data2 ? 1:0; my $i; for( $i = 0; $i < $size; $i++){ if ($data1[$i] ne $data2[$i]){ printf "%s is mismatch with %s\n",$data1[$i],$data2[$i]; } else { printf "%s is match with %s \n",$data1[$i],$data2[$i]; } } print "$result\n" if (!$ori);
apple is match with apple File 2 has missing data
Another method I have tried by comparing the arrays one by one. But there is a tricky part. What if Text 1 has one line while Text 2 has two lines?

Replies are listed 'Best First'.
Re: Perl delete function
by wind (Priest) on Dec 23, 2013 at 07:04 UTC
    It's still not clear what exactly you want, but maybe you want a line by line comparison of the files? If so, the following will help you along:
    use strict; use warnings; open my $fh1, '<', 'text1' or die $!; open my $fh2, '<', 'text2' or die $!; while (!eof($fh1) && !eof($fh2)) { chomp( my $line1 = <$fh1> ); chomp( my $line2 = <$fh2> ); if ($line1 eq $line2 ) { print "Match for $line1\n"; } else { print "Difference for $line1, $line2\n"; } } if (! eof($fh1)) { warn "Still more data in file handle 1\n"; } if (! eof($fh2)) { warn "Still more data in file handle 2\n"; } close($fh1); close($fh2);

    - Miller

    * Edited to add validation for equal length files.


      Yes, I wish to compare it line by line. But there is a tricky part. What if Text1 contains only 1 line and Text2 contains 2 lines? Any suggestion would be much appreciated.

        That is why my code tested for eof (end of file) for both file handles. To complete the validation, verify that it reached the eof for both handles after the while loop.

        *edited my code to demonstrate*

Re: Perl delete function
by taint (Chaplain) on Dec 23, 2013 at 06:30 UTC

    Using the information you provided above. I attempted to re-create the code you asked us to help you with. But I was unsuccessful. Would you be able to produce it for us?


    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      Please refer to my updated code. Many thanks.
Re: Perl delete function
by Anonymous Monk on Dec 23, 2013 at 06:23 UTC
    Not clear! Can you show us your code?
      Please refer to my updated post. Thanks.
        Keys in hashes are unique, so you never had more than one "apple" in hash %m2.

        But it's still unclear which logic you expect, because thats a perfect set operation.

        Could it be that you want to compare arrays one by one by position?

        Then please use a loop to do so.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1068138]
Approved by taint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2023-01-31 00:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found