in reply to Merge the difference between two files and save it on the first file. Also update the first file with info in second file.

> Could someone kindly guide me to the right direction.

Sure! :)

Firstly start with an example that makes sense (why is "world" excluded, it's different!?!) and then show us what you tried so far!

Cheers Rolf

( addicted to the Perl Programming Language)

  • Comment on Re: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.

Replies are listed 'Best First'.
Re^2: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.
by Shariq (Initiate) on Oct 11, 2013 at 03:24 UTC
    Hi! I cam across this example which is supposed to put the difference of the two files in b_array. Then i was thinking of merging it. But it still doesn't solve my problem of updating the file with latest values.
    use strict; use warnings; use constant { FILE_A => './kma', FILE_B => './kma2', }; my %a_hash; my @a_array; my @b_array; open my $a_fh, "<", FILE_A; while ( my $line ~= <$a_fh> ) { chomp $line; $line = /^(.+?);/; # This strips the first field from the line $a_hash{$1} = 1; # Now, I'll use the first field as my index + to my hash push @a_array, $line; # This adds the line to the end of the arra +y } close $a_fh; open my $b_fh, "<", FILE_B; while ( my $line = <$b_fh> ) { $line ~= /^(.+?);/; if ( not exists $a_hash{$1} ) { push @b_array, $line; } } close $b_fh; my @combined_array = sort (@a_array, @b_array); print "content of my combined array = @combined_array";
Re^2: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.
by Shariq (Initiate) on Oct 11, 2013 at 03:41 UTC
    "world" is excluded because it has been updated by the second file.