Link to home
Start Free TrialLog in
Avatar of nparthi
nparthi

asked on

Reading selective values from *.csv file in Perl

Hi,
I have a *.csv file that looks like this. I need to ignore Rows 1-5
and start reading from Row 6. From Row 6 I need only Column1, Column
3, Column4 and so on....(has about 35 values in each row).


RED - Letters in Red auto-populate columns from left to right.            
Color Code - Yellow Field name boxes in yellow are fields required
Color Code - Gray Field name boxes in gray are fields are optional            
Color Code - Green Field name boxes in green are optional fields
Color Code - Orange Field name boxes in orange indicate d - refer etc
44383      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT
44384      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT       
44385      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT


Previously, I used to copy manually the values I wanted to the
input.csv file. But, now I want to read directly from the source file
and avoid the extra step.

My code looks like this

open FILE, "input.csv";

while (<FILE>) {
      chomp;
      my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
}
ASKER CERTIFIED SOLUTION
Avatar of josephfluckiger
josephfluckiger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FishMonger
Your example csv file doesn't have any commas!

open FILE, "input.csv";

for (1..5) {$junk = <DATA>; undef $junk;}  # Throw away the first 5 rows

while (<DATA>) {
   chomp;
# use this next line only if you don't use the previous for loop
   next if ($. < 6)
   @col = split ",";  # split the row and put it into an array
# process the array
}
Oops, I used the built-in DATA file handle and forgot to change that part of the code to FILE when I posted.