Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading selective values from *.csv file in Perl

Posted on 2003-12-08
3
Medium Priority
?
771 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 80 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

730 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