Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Opening multiple output files within a loop

by haukex (Abbot)
on Dec 19, 2017 at 11:52 UTC ( #1205853=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1205853]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-07-23 14:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (469 votes). Check out past polls.