Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: difference between two files

by varalaxmibbnl (Acolyte)
on Dec 19, 2013 at 10:59 UTC ( #1067806=note: print w/ replies, xml ) Need Help??


in reply to Re: difference between two files
in thread difference between two files

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; }


Comment on Re^2: difference between two files
Download Code
Re^3: difference between two files
by marto (Chancellor) on Dec 19, 2013 at 11:05 UTC
      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*

        "Seriously? Which errors? How does adding that text to the start of the OP's program help him solve his problem?"

        The errors generated from running the code presented simply when adding use strict; use warnings;. Does the reasoning in Use strict and warnings not ring true to you?

        "As opposed to opening the files he wants to read, as he does already?"

        Without returning $! on error, I'd advise getting into the habit of using 3 argument open, safer, cleaner.

        From their previous posts they do these things in some examples, and not in others, the coding style is different suggesting they're copying and pasting code from elsewhere, don't understand what it's doing and post 'problems' here.

        If the goal is to encourage someone who doesn't seem to understand the basics of the language to actually learn the language, how best to avoid or report errors the advice you question still seems sound to me.

Re^3: difference between two files
by roboticus (Canon) on Dec 19, 2013 at 11:15 UTC

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-10-21 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls