Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: difference between two files

by 2teez (Priest)
on Dec 19, 2013 at 14:04 UTC ( #1067819=note: print w/ replies, xml ) Need Help??


in reply to difference between two files

Hi varalaxmibbnl,
Several good advice has been given, however comparing the content of the files you presented in your original post.
I can see that file1 is contained in file2 in such a way that if the two files are saved in different arrays, one can use splice to get the difference of the files like:

use warnings; use strict; use Inline::Files; my @file1 = <DATA1>; # updated my @file2 = <DATA2>; # updated print $_ for splice @file2, $#file1 - 1, $#file2; __DATA1__ hi 1 hw r u hi 2 hw r u __DATA2__ hi 1 hw r u hi 2 hw r u hi 3 hw r u
NOTE:
This might not work like you wanted, if the lines of the first file doesn't follow just like that of the second file.

Updated: Change chomp(my @file1 = <DATA1>);
chomp is really not needed here, as pointed out by hdb

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: difference between two files
Select or Download Code
Re^2: difference between two files
by hdb (Parson) on Dec 19, 2013 at 14:30 UTC

    If you remove the chomp statements, you can also remove "$_, $/ for" from the print statement.

      Yes Oooo, o yes, but don't you think one is cryptic enough with the given code already, than adding some more?
      I think it would be better to play clean a bit some of the time.
      Remove the newline character using the chomp and then re-insert when printing finally.

      hdb, on a second thought, you are right. chomp here is really not useful.
      Thanks, am updating my previous post.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
Re^2: difference between two files
by kcott (Abbot) on Dec 20, 2013 at 06:44 UTC

    While not a bad solution (I gave it an upvote), here's a couple of points to consider:

    • You've added two blank lines between the __DATA1__ and __DATA2__ blocks. As a result, you've needed the "$#file1 - 1" calculation.
    • I think using splice here is overkill: a simple array slice would suffice.

    Putting those two points into effect:

    #!/usr/bin/env perl use strict; use warnings; use Inline::Files; my @file1 = <DATA1>; my @file2 = <DATA2>; print for @file2[@file1 .. $#file2]; __DATA1__ hi 1 hw r u hi 2 hw r u __DATA2__ hi 1 hw r u hi 2 hw r u hi 3 hw r u

    Output:

    hi 3 hw r u

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-07-29 16:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls