Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: difference between two files

by Corion (Patriarch)
on Dec 19, 2013 at 10:30 UTC ( [id://1067803]=note: print w/replies, xml ) Need Help??


in reply to difference between two files

What code have you written so far?

This is a faq, see perlfaq4 on "duplicate".

Alternatively, see Algorithm::Diff.

Maybe you want to tell us in more detail what makes the last five lines special.

Replies are listed 'Best First'.
Re^2: difference between two files
by varalaxmibbnl (Acolyte) on Dec 19, 2013 at 10:59 UTC

    i tried with this but i'm not getting the required output

    #!/usr/bin/perl open (FILE,"file1"); @cnt_file = <FILE>; $cnt = @cnt_file; print "$cnt\n"; open (MYFILE,"file2"); $line = <MYFILE>; foreach $line (<MYFILE>) { print "$line" if $. >= $cnt; }

      varalaxmibbnl:

      When you have a problem like this, you should add some print statements to show your variables, so you can verify that they hold what you're expecting. In this case, try adding $. to your existing print statement, like this:

      print "$., $line"

      When you do so, your output is:

      10 15: 1 15: hw 15: r 15: u 15: hi 15: 2 15: hw 15: r 15: u 15: hi 15: 3 15: hw 15: r 15: u

      So you can see that the line number is 15 for every iteration through your for loop. Why is that? It turns out that when you use foreach it wants a list in the parenthesis, so it reads the entire file at once. It then gives you one line at a time.

      Generally when processing a file and want to do it one line at a time, we do it like:

      while (my $line = <MYFILE>) { ... code in loop ... }

      There are some other issues in your code, but I'll let your instructor clarify that.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        use strict; use warnings; Use strict and warnings (then fix the errors)
        Seriously? Which errors? How does adding that text to the start of the OP's program help him solve his problem?
        use open properly
        As opposed to opening the files he wants to read, as he does already?

        *sigh*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-16 17:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found