use strict; use warnings; my @array = ; foreach my $element (@array) { chomp $element; my ($fname, $line_number, $content) = split /\s*:\s*/, $element; open my $outfh, '>>', $fname or die "Cannot open $fname: $!\n"; print $outfh $content; # You didn't tell us what is supposed to happen with $line_number. close $outfh or die "Couldn't close $fname after writing: $!\n"; } __DATA__ tests/right/case1: 12 : //comment tests/right/case1: 13 : //comment test/right/case3: 5 : //comment