use strict; open FILE, "; close FILE; open OUTPUT ">outputfile" or die "Hcrelb: $!\n"; for my $d (1..10) { print OUTPUT join $\, grep (/^$d/, @contents); } close OUTPUT;