use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new; open my $csv_file, "<", "DATA.csv" or die "Can't open CSV file: $!\n"; while (my $row = $csv->getline($csv_file)) { my($significator_and_file) = clean($row->[0]); my ($significator, $file) = split /\s+/, $significator_and_file; next unless $significator and $significator eq 'false'; open (my $per_line_fh, ">", "$file.csv" ) or die "Can't open per-line file $file: $!\n"; shift @$row; print $per_line_fh "$file:\n"; for my $i(map {clean($_)} @$row) { my $d = "$i\n"; print $per_line_fh $d if ($i); } close $per_line_fh; } sub clean { my $string = shift; $string =~ /\s*,\s*/; $string =~ s/\s+$//; return $string; }