Solved

Reading selective values from *.csv file in Perl

Posted on 2003-12-08
3
770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

615 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