Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Merge the difference between two files

by hopper (Novice)
on Jun 16, 2017 at 18:07 UTC ( [id://1192973]=note: print w/replies, xml ) Need Help??


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

Hi NetWallah, I really like the code that you wrote. However It is not printout out the way I want.

Currently, it finds the line "NAME" if match then merge the content. however, I want to separate two files and just merge unmatched

line/content "NAME" that is in file 2 to file#1. If the line "NAME" match ignore the content of the lines. can I print the result to a

file and not on the display only?

For example:(ignore the content, if line "NAME, VAR1, VAR2" match)

File 1: NAME, VAR1, VAR2 apple, mange File 2: NAME, VAR1, VAR2 jack fruit, banana

Replies are listed 'Best First'.
Re^3: Merge the difference between two files
by poj (Abbot) on Jun 16, 2017 at 19:36 UTC

    Try this, still using hash-of-arrays

    #!/bin/perl -w use strict; use warnings; # input my %hash=(); my @filename = ('File1.txt','File2.txt'); for my $n (0..1){ parse($n,$filename[$n]); }; output('output.txt'); # parse sub parse { my ($n,$filename) = @_; open my $fh,'<',$filename or die "$!"; my $key; while (<$fh>){ if (/^NAME/){ $key = $_; $hash{$key}[$n] = ''; } else { $hash{$key}[$n] .= $_ if $key; } } close $fh; } # merge sub output { my ($filename) = @_; open my $fh,'>',$filename or die "$!"; for my $key (sort keys %hash){ if (defined $hash{$key}[0]){ print $fh $key.$hash{$key}[0]; } else { print $fh $key.$hash{$key}[1]; } } close $fh; }
    poj
Re^3: Merge the difference between two files
by NetWallah (Canon) on Jun 17, 2017 at 04:53 UTC
    To achieve this, after the line
    $currentheader = $line;
    add this:
    if (exists $result{$currentheader}){ $currentheader="RECYCLE_BIN"; }
    I will leave it as an exercise for you to figure hou how to avoid printing the "RECYCLE_BIN" item.

                    Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

      I was able to print the unmatched contents to a file. could you please help me edit the code to do just compare the content of the line "NAME" and print out the header of the file A. Don't compare the headers. I tried to figure out but so far I have no luck. Thanks so much in advance

        Your second and third sentences contradict each other:
        * edit the code to do just compare the content of the line "NAME" * Don't compare the headers
        So I'm having trouble understanding what you want.

        Sample data, and expected results would help.

                        Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

Re^3: Merge the difference between two files
by huck (Prior) on Jun 16, 2017 at 18:40 UTC

    can I print the result to a file and not on the display only?

    Did you even try to figure this out by yourself or are you just looking for someone to do all your work for you?

    https://perldoc.perl.org/functions/select.html
    select FILEHANDLE

    If FILEHANDLE is supplied, sets the new current default filehandle for output. ... , a write or a print without a filehandle default to this FILEHANDLE.

Log In?
Username:
Password:

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

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

    No recent polls found