#!/usr/bin/perl

use Tie::File;

sub search_replace {
	my ($file_name1, $file_name2, $lines_file1, $lines_file2) = @_;
	chomp $file_name1;
	chomp $file_name2;
	print "\nfile_name1 (read) is $file_name1 \nfile_name2 (write to) is $file_name2 \n"; 
	$j=0;
	$i=0;
	$found="false";
	tie @file1_array, 'Tie::File', $file_name1 ||  die "problem using tie module:($!)";
	tie @file2_array, 'Tie::File', $file_name2 ||  die "problem using tie module:($!)";

	while ($i != $lines_file1)
	{
#catch all the comments before splitting

#		if ($file2_array[$j] =~ /^#|\n/)
#		{
#		$j++;
#		next;
#		}
#
#		if ($file1_array[$i] =~ /^#|\n/)
#		{
#		push @file2_array, $file1_array[$i];
#		$i++;
#		next;
#		}
	@cellule1_array = split (/: /,$file1_array[$i]);
	print "\ncellule1_array[0] is $cellule1_array[0]  ????   $cellule1_array[1]";
	while ($j <= $lines_file2 || $found ne "true")
	{
	if ($file2_array[$j] =~ $cellule1_array[0])
		{
		@cellule2_array = split (/: /, $file2_array[$j]);
		if ($cellule2_array[0] eq $cellule1_array[0])
			{
			print "\t$cellule1_array[0] has been found";
			$found="true";
			$file2_array[$j]=$file1_array[$i];
			last;
			}
		}
		$j++;
		}
	if ($j > $lines_file2 || $found eq "false")
		{
		push @file2_array, $file1_array[$i];
		}
	$j=0;
	$i++;
	}
	untie @file2_array;
	untie @file1_array;	
}
