Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: difference between two files

by roboticus (Chancellor)
on Dec 19, 2013 at 11:15 UTC ( [id://1067810]=note: print w/replies, xml ) Need Help??


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

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1067810]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found