http://www.perlmonks.org?node_id=1057740


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.

Hi,

read the first file line by line. Split the line at the '='. Take the first part as key of a hash and the second as value of that key. Then do the same for the second file. When you hit in the second file a key which was already read than the value gets substituted. When you have read both files print every tuple (key, value) of your resulting hash to the first file.

When you have problems with some of that subtasks show us your code and we'll help you.

McA

  • 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 09:37 UTC
    Thanks guys. Especially McA and Corion. I have this figured. This is what I have done so far and it seems to work. Please suggest any improvements or blunders that I am making with the code.
    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 (<$a_fh>) { chomp; my ($key, $value) = split '=', $_, 2; $a_hash{$key} = $value; } open my $b_fh, "<", FILE_B; while (<$b_fh>) { chomp; my ($key, $value) = split '=', $_, 2; $a_hash{$key} = $value; } open my $c_fh, ">", FILE_A or die $!; print $c_fh "$_=$a_hash{$_}\n" for (keys %a_hash);
      > Please suggest any improvements

      Your example suggested that you want sorted output

      print $c_fh "$_=$a_hash{$_}\n" for (sort keys %a_hash);

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      Just some suggestions

      The use constant part is not needed probably, you can add the filename in the open line (saving some typing).

      You have two pairs of variables with the same name for file A and File B ($key, $key, $value, $value). This is probably a source of future troubles.

      You don't need to repeat the word array as variable name, if something begins with @ you know yet that is an array, something like "@first" is probable a better choice, (easier to write and read) than "@a_array". Don't use @a either.

      You need to add possible spaces to the split regex, and you could probably fill the %hash directly with maybe something like: (untested)

      my ($key, $a_hash{$key}) = split /\s*=\s*/, $_, 2;
      Nicely done.

      Funny that it felt like a homework assignemnt, showing that homework sometimes does reflect the real world :-).

      That said, I can't see any particular issue with yoru code, other than the use of a variable for the file handle, which I'm seeing more and more of in Perl posts here. Has that become the "new normal"?

      What you just did with that code, by the way, is essentially re-invent the sort/merge using Perl hashes to take care of all the dirty work for you. :-)

      Looking forward to your next Perl issue.

        Thanks. But i have some issues with the code though. Its not perfect in the sense that when I have space between my configuration parameters, it gets messed up. For example:
        if this was my first file:
        A=hello

        B=world
        The I would have trouble reading and hashing. So I am limited in the sense that my files cant have new line in between the configuration settings.