in reply to Import comma delimited file into an array

if the primary goal is to filter, then use a command line grep and redirect the filtered output into a file; instead of reading every line into an array or hash and rejecting mostly.
the hardest line to type correctly is: stty erase ^H
  • Comment on Re: Import comma delimited file into an array

Replies are listed 'Best First'.
Re^2: Import comma delimited file into an array
by drodinthe559 (Monk) on Nov 18, 2008 at 22:33 UTC
    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" } }
      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; }
        Hi toolic! Why close the file, if it was open with lexical filehandle (and should be closed when scope goes away?)
        []s, HTH, Massa (κς,πμ,πλ)
        Thanks your suggestions are extremely helpful which I will implement. Thanks again. Dave
Re^2: Import comma delimited file into an array
by Zen (Deacon) on Nov 19, 2008 at 16:25 UTC
    And the hardest line to read correctly...ow.