Leser: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/perl use strict; #use warnings; #use diagnostics; my $file = "test.txt"; my $out1 = "chr1.gens"; my $out2 = "chr2.gens"; my $out3 = "chr3.gens"; open(IN,'<'.$file) || die "Can not open file $file: $!"; open OUT1, "> $out1" or die "Can't open $out1 : $!"; open OUT2, "> $out2" or die "Can't open $out2 : $!"; open OUT3, "> $out3" or die "Can't open $out3 : $!"; while(<IN>){ if ($_ =~ m/^0/){ print OUT1 "$_\n"; } elsif ($_ =~ m/^1/){ print OUT2 "$_\n"; } elsif ($_ =~ m/^2/){ print OUT3 "$_\n"; } } close IN; close OUT1; close OUT2; close OUT3;
1 2 3 4 5 6
my @fh = (\*OUT1, \*OUT2, \*OUT3); if (m/^([012])/) { my $index = $1; my $fh = $fh[$index]; print $fh "$_\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
!/usr/bin/perl use strict; use warnings; my @output = ( { file => "chr1.gens", pattern => qr/^0/ }, { file => "chr2.gens", pattern => qr/^1/ }, { file => "chr3.gens", pattern => qr/^2/ }, ); my $file = "test.txt"; open(IN,'<'.$file) || die "Can not open file $file: $!"; # open the output handles foreach my $o (@output) { my $file = $o->{file}; open(my $handle, "> $file") or die "Can't open $file: $!"; $o->{handle} = $handle; } while (my $line = <IN>) { foreach my $o (@output) { my $pattern = $o->{pattern}; if($line =~ $pattern) { print $o->{handle} $line; # oder eben print $o->{handle} "$line\n"; # falls das zusätzlich \n erwünscht ist } } } # close the output handles foreach my $o (@output) { close $o->{handle}; } close IN;
2010-05-04T11:49:54 esskarCode (perl): (dl )print $o->{handle} $line;
1 2 3 4 5
my $fh = $some_hash{...}; print $fh "foo"; # oder print { $some_hash{...} } "foo";