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


In reply to Re: Suggestions to make this code more Perlish by kcott
in thread Suggestions to make this code more Perlish by ricardo_sdl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.