Solved

Reading selective values from *.csv file in Perl

Posted on 2003-12-08
3
753 Views
Last Modified: 2012-06-21
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 (/\,/);
}
0
Comment
Question by:nparthi
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
josephfluckiger earned 20 total points
ID: 9898901
How about this:
-------------------------------
open FILE, "input.csv";
my $rowCount=0;
while (<FILE>) {
     $rowCount++;
     next if $rowCount < 6; # this skips the first 5 rows.
     chomp;
     my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
}
-------------------------------

Does the raw input file have commas in it, or tab characters (your sample file doesn't , but your code does).
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9899344
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
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9899398
Oops, I used the built-in DATA file handle and forgot to change that part of the code to FILE when I posted.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now