#!/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"; } }