in reply to Opening multiple output files within a loop
use warnings; use strict; my @labels = qw/ foo bar quz /; my %fh; for my $label (@labels) { my $filename = "out_${label}.txt"; open $fh{$label}, '>', $filename or die "$filename: $!"; } while (<DATA>) { # or whatever condition you like, just a demo here if (/lbl_(foo|bar|quz)/) { print { $fh{$1} } $_; # how to print to one of these handles } else { print $_ } # just a default action, adjust as needed } close $_ for values %fh; __DATA__ Hello lbl_foo lbl_bar World Test lbl_quz lbl_foo A No label here
|
|---|