#!/usr/bin/perl use strict; my $file1 = < ) { $key = $1 if ( s/^(Iteration \d\d?):// ); push( @{ $record{$key} }, $1 ) if ( /(\d:\s.*)/ ); } close( $file_1 ); } while (my $line = ) { 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