I have two files in the following formats
File1.txt
[name]
value1 = "abc"
value2 = "def"
value3 = "valtoget1"
value4 = "jkl"
value5 = "mno"

[name]
value1= "pqr"
value2 = "stv"
value3 = "valtoget3"
value4 = "jsjs"
value5 = "hshs"

File2.txt
[name]
value1 = "abc"
value2 = "def"
value3 = "valtoget2"
value4 = "value4"
value5 = "value5"

[name]
value1= "pqr"
value2 = "stv"
value3 = "valtoget4"
value4 = "fds"
value5 = "sfs"

I need to get the value3 values into a tab separated file format, if value1
and value2 match for each file. They may not be in the same order

like 
valtoget1       valtoget2
valtoget3       valtoget4

The logic I tried using is to use the [name](as it does not change) as a
flag to check values in a hash of File1 and then iterate through File2 for
each section as in code below. The logic seems to be flawed as it does not
parse through the first file as required.
Is there a better approach to take? The file is very large and ideally ,
slurping into an array may turn out to be an issue. 


use strict;

my $File1 = "File1.txt";
my $File2 = "File2.txt";
my $File3 = "msg.txt";

my %ValueArray =();
my %OtherValueArray=();
my $Value1Flag = 0;
my $Value2Flag = 0;

my $FirstFile;
my $SecondFile;

open (OF, "$File1") or die "Could not open $File1\n";
open (IF, "$File2") or die "Could not open $File2\n";
open (EF , "> $File3") or die "Could not open $File3\n";

# Open the first file here
line: while  ($FirstFile=<OF>) {
        chomp($FirstFile);
    if ($FirstFile=~m/\[name\]/i) {
        # for handling the first line 
                if (%ValueArray == ()) {
            next line;
        }

                # open the second file
        while ($SecondFile=<IF>) {
            chomp($SecondFile);
            if ($SecondFile=~m/\[name\]/i) {
                
                if ($ValueArray{'Value1'} eq $OtherValueArray{'Value1'}) {
                    $Value1Flag = 1;
                }
                
                if ($ValueArray{'Value2'} eq $OtherValueArray{'Value2'}) {
                    $Value2Flag = 1;
                }
                
               
                
                if ($Value1Flag == 1 && $Value2Flag == 1 ) {
                    print EF
"$ValueArray{'Value3'}\t$OtherValueArray{'Value3'}\n";
                    
                }
                                
                my %OtherValueArray = ();  
                my $Value1Flag = 0;
                my $Value2Flag = 0;
                next;  
            }    
            
                        # if the line is not [name], push into a hash
            $SecondFile=~s/^\s+//;
            $SecondFile=~s/\s+$//;
            my ($OtherKey, $OtherValue) = split ('\s*=\s*',$SecondFile,2);
            $OtherValueArray{$OtherKey} = $OtherValue;
        }
       
    my %ValueArray = ();  
    next;  
    
    }    
    
        # if the line is not [name], push into a hash
    else {    
        $FirstFile=~s/^\s+//;
        $FirstFile=~s/\s+$//;
        my ($Key, $Value) = split ('\s*=\s*',$FirstFile,2);
        $ValueArray{$Key} = $Value;
    }
}

close (EF);
close (IF);
close(OF);

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to