Re: Import comma delimited file into an array
by jeffa (Bishop) on Nov 18, 2008 at 18:07 UTC
|
If you aren't concerned with safety -- then just use split on a single comma -- you can even ignore whitespace:
my @line = split( '\s*,\s*', $line );
You'll end up with a new line in the last element of your array, but that can be chomped off.
However, i strongly recommend using Text::CSV for this task (or Text::CSV_XS).
| [reply] [d/l] [select] |
|
|
Now, if I import more than one row, don't I need to bind it?
| [reply] |
|
|
| [reply] |
Re: Import comma delimited file into an array
by ccn (Vicar) on Nov 18, 2008 at 18:09 UTC
|
| [reply] |
|
|
Here's my code if you want to take a look at it.
use File::Glob;
my @files = glob("C:/Wire/*.csv");
foreach my $x (@files) {
open FILE, $x;
while (<FILE>) {
my @temp = split(/,/, $_);
next if (substr($_,0,5) =~ /Debit/) ;
print $_;}
}
| [reply] [d/l] |
|
|
right choice is this module Text::CSV
| [reply] |
Re: Import comma delimited file into an array
by 23skiddoo (Beadle) on Nov 19, 2008 at 15:15 UTC
|
I do a LOT of CSV parsing and like to use Text::Parsewords. With that, you can do something like:
use Text::Parsewords;
my $delim = ",";
while (<>) {
my @rec = &parse_line( $delim, 0, $_ );
# then do something with @rec...
}
Hope that helps!
| [reply] [d/l] |
Re: Import comma delimited file into an array
by aquarium (Curate) on Nov 18, 2008 at 21:16 UTC
|
| [reply] |
|
|
I updated my code again. You will probably see what I'm trying to do.
Let me know if there is a better or more efficient way of doing it.
use File::Glob;
my @files = glob("C:/Wire/*.csv");
open OUTPUT, ">C:/Wire/WO.txt";
foreach my $x (@files) {
open FILE, $x;
while (<FILE>) {
my @row = split(/,/, $_);
next if (substr($_,0,5) =~ /Debit/) ;
print "{1100}02P N" . $row[6] . "{1110}" . "{1120}" . "{1510}" . "
+{1520}" . "{3320}" . "{3400}" . "{3600}" . "{4320}" .
"{5000}" . "\n";
print OUTPUT "{1100}02P N" . $row[6] . "{1110}" . "{1120}" . "{151
+0}" . "{1520}" . "{3320}" . "{3400}" . "{3600}" . "{4320}" .
"{5000}" . "\n"
}
}
| [reply] [d/l] |
|
|
Here are some improvements I would make:
- use warnings; use strict;
- Use lexical filehandles $fh.
- Use the 3-argument form of open
- Check the return value of open with die
- Check for 'Debit' immediately
- Use eq instead of a regex to check for 'Debit'
- Just grab the 7th element from the array returned by split
- Eliminate most of those concatenation operators
- close the file
use strict;
use warnings;
use File::Glob;
my @files = glob("C:/Wire/*.csv");
for my $file (@files) {
open my $fh, '<', $file or die "can not open file $file: $!";
while (<$fh>) {
next if (substr($_,0,5) eq 'Debit');
my $row6 = (split /,/)[6];
print "{1100}02P N", $row6, "{1110}{1120}{1510}{1520}{3320}{3400
+}{3600}{4320}{5000}\n";
}
close $fh;
}
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
And the hardest line to read correctly...ow.
| [reply] |