Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-01-21 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (227 votes). Check out past polls.

    Notices?