I've read about the Text File Format(https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii-delimited-text-not-csv-or-tab-delimited-text) and decided to create a perl script to convert a csv file to this format.

Instead of using one of the available modules(like Text::CSV) to parse the csv I decided to do it myself as matter of practice.

I'm starting to learn perl and I ask for your wisdom on how to make this code better.

Updated code after some suggestions.

#!/usr/bin/perl use 5.010; use strict; use warnings; use utf8; use open qw(:std :utf8); #Convert a csv file to text file format #https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii +-delimited-text-not-csv-or-tab-delimited-text #the csv file is expected to use double quotes as the delimiter charac +ter and utf-8 encoded sub is_complete_line { my ($line) = @_; my $inside_double_quotes; my $opening_double_quotes = 0; my $closing_double_quotes = 0; foreach(split('', $line)) { next if $_ ne '"'; if ($inside_double_quotes) { $inside_double_quotes = ! $inside_double_quotes; $closing_double_quotes++; next; } $opening_double_quotes++; $inside_double_quotes = ! $inside_double_quotes; } $opening_double_quotes == $closing_double_quotes; } sub convert_to_ttf { my ($line) = @_; my $inside_double_quotes; my $converted_line = ''; foreach(split('', $line)) { if ($_ eq '"' && $inside_double_quotes) { $inside_double_quotes = 0; next; } if ($_ eq '"' && ! $inside_double_quotes) { $inside_double_quotes = 1; next; } if ($_ eq "," && ! $inside_double_quotes) { $converted_line .= "\x{1F}";#unit separator next; } if ($_ eq "\n" && ! $inside_double_quotes) { $converted_line .= "\x{1E}";#record separator next; } $converted_line .= $_; } $converted_line; } open my $fh, '>', "output.txt" or die "Couldn't create the output file +.\n"; my $line = ''; while (<>) { $line .= $_; if (is_complete_line($line)) { print $fh convert_to_ttf($line); $line = ''; } } close $fh; print "Conversion done.\n";

Here is a link to a sample csv file that you can convert(https://db.tt/GWrID417).


In reply to 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.