foreach (keys %fhs) { my $fh = $fhs{$_}; if ($_ =~ /\.h\.new$/) { print $fh "#endif\n"; } #### for my $file (keys %fhs) { # ... print $fh whatever if $file =~ /\.\.new$/; } #### for (keys %fhs) { # ... print $fh whatever if /\.\.new$/; }