my @content; foreach ('','.1','.2','.3','.4') { open(my $fh,'<',$string.$_) or die "Could not open $string.$_ : $!\n"; push @content, scalar <$fh>; } if ($content[0] == $x){ print @content; $x++; }