In the first file, I basically store the data in a hash of array structure where I used 'Iteration...' as my key. When I loop through the second file, I would simply check if the key exists and if it does, then it should print the corresponding data.
#!/usr/bin/perl
use strict;
my $file1 = <<END_FILE2;
Iteration 1:
0: Data: 1023 1023
1: Data: 200 1023
2: Data: 300 1023
3: Data: 250 1023
4: Data: 1023 1023
Iteration 2:
0: Data: 1023 1024
1: Data: 1023 60
2: Data: 1023 90
3: Data: 1023 100
4: Data: 1023 1023
END_FILE2
my (%record, $key);
for my $i (\$file1) {
open( my $file_1, '<', $i ) or die "cannot open file";
while( <$file_1> ) {
$key = $1 if ( s/^(Iteration \d\d?):// );
push( @{ $record{$key} }, $1 ) if ( /(\d:\s.*)/ );
}
close( $file_1 );
}
while (my $line = <DATA>) {
if ( $line =~ s/^(Iteration \d\d?):// ) {
$key = $1;
print "$key:\n";
}
my ($index) = $line =~ /^(\d)/;
print "File1:$record{$key}->[$index] File2:$line"
if ( defined( $index ) );
print "\n" if ( $index == $#{ $record{$key} } );
}
__DATA__
Iteration 1:
0: Data: 1023 1023
1: Data: 250 1023
2: Data: 60 1023
3: Data: 99 1023
4: Data: 1023 1023
Iteration 2:
0: Data: 1023 1024
1: Data: 1023 60
2: Data: 1023 90
3: Data: 1023 100
4: Data: 1023 1023