trolis has asked for the wisdom of the Perl Monks concerning the following question:
I have written a simple perl script (which works just fine) to open csv file, skip the first two lines of a header and format the columns from: 400;0,34567; to --> 400 0.34567 (change semicolon to space & comma to dot) I would appreciate your ideas on how to improve the following code:
#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new; open (CSV, "<File_name.csv") or die $!; while (<CSV>) { next if (1 .. 2); $_ =~ s/\;/ /g; $_ =~ s/,/\./g; if ($csv->parse($_)) { my @columns = $csv->fields(); print "@columns\n"; } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } } close CSV;
Somehow I have a feeling that this code is not efficient but it works. Is it worth to improve it or just use it as it is? I know it is possible to do this simple character replacement directly in vim using sed or with awk but I want to use Perl instead. Thank you!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ideas on how to improve existing code
by GrandFather (Saint) on Aug 29, 2011 at 09:55 UTC | |
| |
|
Re: ideas on how to improve existing code
by Tux (Canon) on Aug 29, 2011 at 13:39 UTC | |
by GrandFather (Saint) on Aug 29, 2011 at 20:41 UTC | |
by Tux (Canon) on Aug 29, 2011 at 22:58 UTC | |
by trolis (Novice) on Aug 29, 2011 at 18:48 UTC | |
|
Re: ideas on how to improve existing code
by Anonymous Monk on Aug 29, 2011 at 09:02 UTC | |
by trolis (Novice) on Aug 29, 2011 at 09:31 UTC | |
|
Re: ideas on how to improve existing code
by JavaFan (Canon) on Aug 29, 2011 at 11:27 UTC | |
by trolis (Novice) on Aug 29, 2011 at 12:01 UTC | |
|
Re: ideas on how to improve existing code
by i5513 (Pilgrim) on Aug 29, 2011 at 13:23 UTC | |
by dsheroh (Monsignor) on Aug 30, 2011 at 09:20 UTC |