my @sorcefiles; my @tablefiles; while (@data = $sth->fetchrow_array) { push(@sorcefiles,\@data) if($data[0] eq 'PSAPSOURCEI'); push(@tablefiles,\@data) if($data[0] eq 'PSAPSTABD'); } for my $line (@tablefiles) { $line->[1]=~m!^(.+sapdata\d+/)!; my $path=$1; my @sources; for my $file (@sorcefiles) { push(@sources,$file) if($file->[1]=~/\Q$path\E/); } my $filesum=0; for my $file (@sources) { $filesum+=-s $file->[1]; } my $sum=$line->[-1]-$line->[-2]; if($sum-$filesum < 10) { # mach was ... } }