Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Perl delete function

by wind (Priest)
on Dec 23, 2013 at 07:04 UTC ( [id://1068149]=note: print w/replies, xml ) Need Help??


in reply to Perl delete function

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.

Replies are listed 'Best First'.
Re^2: Perl delete function
by annel (Novice) on Dec 23, 2013 at 07:10 UTC
    Greetings!

    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*

        Thanks for tips. It works fine! I never knew about eof before until now. New thing to pick up.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1068149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2026-04-21 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.