There is no efficiency issue of note. In particular, if it does the job you need to do fast enough it is sufficiently efficient. However there is a bunch of stuff that can be improved.

For a start you should always use the three argument version of open and lexical file handles:

open my $csvIn, '<', $fileName or die "Failed to open $fileName: $!";

It also helps if error strings provide more than just the bare minimum of information. For an open including the file name that was being used (rather than hoping it is what you think it ought to be) helps a huge amount.

Skipping the first two lines in the loop that handles the rest of the lines is ok, but not near as clear as skipping them outside the loop:

<$csvIn> for 1 .. 2; while (defined (my $line = <$csvIn>)) {

Using translate for simple character translation is fast and clear:

$line =~ tr/;,/ ./;

Although if you were to use regular expressions you should note that there is no need to quote ; in a regular expression string and no need to quote anything in the replacement string (except stuff you'd quote if it were a double quoted string).

Using consistent indentation helps a lot!

I prefer to make function calls explicit by supplying parenthesis even is they aren't strictly required. At the very least you should be consistent (cf: $csv->fields and $csv->error_input).

Taking that all together you'd end up with something like the following (untested) code:

#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new (); my $fileName = 'File_name.csv'; open my $csvIn, '<', $fileName or die "Can't open $fileName: $!"; <$csvIn> for 1 .. 2; while (defined (my $line = <$csvIn>)) { $line =~ tr/;,/ ./; if ($csv->parse ($line)) { my @columns = $csv->fields (); print "@columns\n"; } else { my $err = $csv->error_input; print "Failed to parse line $.: $err"; } } close $csvIn;
True laziness is hard work

In reply to Re: ideas on how to improve existing code by GrandFather
in thread ideas on how to improve existing code by trolis

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.