in reply to Suggestions to make this code more Perlish
G'day ricardo_sdl,
Welcome to the monastery.
Here's a shorter way to do it. It's 14 lines (yours is 75 lines). It's definitely Perlish; I'll leave others to comment on whether it's more Perlish. :-)
#!/usr/bin/env perl use 5.010; use strict; use warnings; use autodie; local $\ = chr 30; open my $csv_fh, '<', 'pm_1080161_input.csv'; open my $tff_fh, '>', 'pm_1080161_output.tff'; while (<$csv_fh>) { chomp; s/(?:"(?<a>[^"]*)"|(?<a>[^,]*)),?/$+{a}\037/g; s/[\037]+$//; print $tff_fh $_; }
Update: Just to summarise the discussion from the six nodes that follow this, here's an improved solution:
#!/usr/bin/env perl use 5.010; use strict; use warnings; use autodie; local $\ = chr 30; open my $csv_fh, '<', 'pm_1080161_input.csv'; open my $tff_fh, '>', 'pm_1080161_output.tff'; my $re = qr{ " (?<field> [^"]* ) " | (?<field> [^,]* ) }x; print $tff_fh $_ for map { chomp; s/$re,/$+{field}\037/g; $_ } <$csv_f +h>;
End update.
See perlre for documentation on any of regex code. Ask if there's anything else you don't understand.
And here's a shorter way to view the output. It's one line vs. your 29 line script (in Re^2: Suggestions to make this code more Perlish).
$ perl -pe 'y/\036\037/\012|/' pm_1080161_output.tff Country|Name|Address|Age Andorra|Aileen, Daquan, Hammett, Malachi|17014|69 Solomon Islands|Cyrus, Giacomo, Gretchen, Curran|76935|26 Czech Republic|Briar, Larissa, Sybil, Colin|29565|88 Japan|Wyatt, Gavin, Derek, Coby|10734|52 Gabon|Tobias, Maya, April, Quintessa|77397|95
The rest of the output is in the spoiler:
-- Ken
|
---|