Hi guys
I have two files as listed below. I want to compare first column from second file with the first column from first file, if exists, give it the same value of second column from the first file as second column for the second file. If not, increase the biggest value of second column of first file by one and add it as a value for second file.
Any tips to start working on.
Thanks in advance

File 1:

C1 Value
123 5
121 4
122 6
212 7

File 2:

C1
123
111
122

124

The results should be

File 1:

C1 Value
123 5
121 4
122 6
212 7

File 2:

C1
123 5
111 8
122 6
124 9

Hmm... If I understood correctly...

What you need to do is to read the 1st files and save data in a variable. It could be in a hash or array, it is up to you. You will need to split the first file columns into value of the 1st and 2nd column before hand. You should also find the highest value of the 1st file and save it in a variable.

From there, open and iterate through your 2nd file and check it against the 1st file data. Change any values if you need to.

Then you will have to overwrite your 2nd file (I guess?). Below is an example of pseudo code. There are other ways to deal with anyway...

=pod
i.e. (Assuming the data file format is always correct/valid
initialize %first_data variable as a hash
initialize $maxval variable as 0
Open to read and iterate through each line in the 1st file
  read a line
  # you may skip if the line is empty
  split the line using white spaces as delimiter
  save data in the hash using the 1st column value as key and
    2nd column as value
  assign 2nd column to $maxval if it is greater than
end open
initialize $outstr as an empty string
Open read and iterate through each line in the 2nd file
  read a line
  # you may skip if the line is empty
  append $outstr with the line and a white space
  if there exists value in $first_data when use the line as key
    append $outstr with the value in $first_data using the key
  else
    increment $maxval by 1
    append $outstr with $maxval
  end if
  append $outstr with a new line
end open
Open to write to the 2nd file
  write $outstr into the file
end
=cut

=pod
i.e. from your example, you should get %first_data & $maxval values as...
%first_data = {'123'=>5, '121'=>4, '122'=>6, '212'=>7}
$maxval = 7
=cut

Hi,
Just like it was pointed out initially. There are more than one way to achieve your desired result.
You could do everything in an hash variable and two while loops or you could use two hash variables and a single while loop.
[Taywin] actually gave you a pseudo code that you can work with.
But below I will show one way of doing what you want. But, you will have to either install one of the module used or you use perl's open function to open the files.

#!/usr/bin/perl 
use warnings;
use strict;

use Data::Dumper;
use Inline::Files;    # install this from CPAN

my %hash = ( high => 0, count => 0, );

while (<FILE1>) {
    chomp;
    next if /[^0-9 ]+|^\s*$/;
    my ( $c1, $value ) = split /\s+/ => $_;
    push @{ $hash{FILE1} } => { $c1 => $value };
    $hash{high} = $value if $hash{high} < $value;
}

while (<FILE2>) {
    chomp;
    next if /[^0-9 ]+|^\s*$/;
    if ( exists $hash{FILE1}->[ $hash{count} ]{$_} ) {
        push @{ $hash{FILE2} } => { $_ => $hash{FILE1}->[ $hash{count} ]{$_} };
    }
    else {
        push @{ $hash{FILE2} } => { $_ => ++$hash{high} };

    }
    $hash{count}++;
}
delete $hash{high};
delete $hash{count};
{
    local $Data::Dumper::Sortkeys = 1;
    print Data::Dumper->Dump( [ $hash{FILE1}, $hash{FILE2} ],
        [qw(FILE1 FILE2)] );
}
__FILE1__
C1 Value
123 5
121 4
122 6
212 7


__FILE2__
C1
123
111
122

124

My result output is:

$FILE1 = [
           {
             '123' => '5'
           },
           {
             '121' => '4'
           },
           {
             '122' => '6'
           },
           {
             '212' => '7'
           }
         ];
$FILE2 = [
           {
             '123' => '5'
           },
           {
             '111' => 8
           },
           {
             '122' => '6'
           },
           {
             '124' => 9
           }
         ];

@2teez. Thanks a lot for your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.