while (<IN>) {
Read the file 'curve12.data' line by line and put the current line in the $_ variable.
if (/^Curve(\d):/.../^\s*$/) {
Look for a block of text that starts with the line matching /^Curve(\d):/ and ends with the line matching /^\s*$/ and store a single numerical digit between 'Curve' and ':' in the $1 variable.
if (defined($1) && $1==1 && /^\d/) {print $c1 "$_\n";}; if (defined($1) && $1==2 && /^\d/) {print $c2 "$_\n"};
If $1 is defined, which it always is because we wouldn't get here unless (\d) matched, and $1 is equal to one and the current line starts with a numerical digit then print the current line with an additional trailing newline to the file 'curve1.out'. Also, if $1 is equal to two then print the current line with an additional trailing newline to the file 'curve2.out'.
Could also be written as:
#!/usr/bin/perl use warnings; use strict; my %out = map { my $name = "curve$_.out"; open my $FH, '>', $name or die "cannot open $name $!"; $_ => $FH; } 1, 2; my $in ='curve12.data'; open my $IN, '<', $in or die "cannot open $in $!"; while ( <$IN> ) { if ( /^Curve(\d):/ .. /^\s*$/ ) { next unless /^\d/; exists $out{ $1 } and print { $out{ $1 } } "$_\n"; } }
In reply to Re: # Translation
by jwkrahn
in thread # Translation
by GiJoe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |