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

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);
  • Comment on 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.
  • Download Code

Replies are listed 'Best First'.
Re^3: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.
by LanX (Saint) on Oct 11, 2013 at 21:36 UTC
    > 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)

Re^3: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.
by marinersk (Priest) on Oct 11, 2013 at 21:15 UTC
    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.
Re^3: Merge the difference between two files and save it on the first file. Also update the first file with info in second file.
by pvaldes (Chaplain) on Oct 14, 2013 at 20:38 UTC

    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;