my $filename = '/path/to/file.csv'; open (my $fh, '>', $filename) or die "Could not open $filename, $!"; my @headers = qw( DET IN JJ NN VBD ); print $fh join(',',@headers) . "\n"; # then, for each of your phrases print $fh join(',', map($tags{$_} || 0, @headers) ) . "\n"; close $fh; #### use Tie::Array::CSV; my $filename = '/path/to/file.csv'; tie my @file, 'Tie::Array::CSV', $filename; # (this bit has been fixed - see comment below) # for each of your phrases my @row = map { $tags{$_} || 0 } @headers; push(@file,\@row); untie @file;