Solved

Reading Excel File

Posted on 2006-11-19
8
138 Views
Last Modified: 2010-03-05
Hi Experts,
              I want to read  .CSV file using PERL, the file has three columns, list1, list2  and list 3. I want to read values of the three columns.
          Can you please help me how i can do this ?


Thanks

SS
0
Comment
Question by:sud
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17976511
while( <> ){
  my($list1,$list2,$list3) = spit/,/;
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 17976539
Not to "spit" on ozo's answer ;-)

while (<>) {
  my ($list1, $list2, $list3) = split /,/;
}

To expand this a bit

#!/usr/bin/perl
use strict;

open CSV, "/path/to/file.csv" or die "Can not open file.csv $!\n";

while (<CSV>) {
  chomp;
  my ($list1, $list2, $list3) = split /,/;
}

Note that you could also read into an array, eg:

@columns = split /,/;

Also note that this simplistic method won't handle all types of CSV data.  For that, you are better to use one of the many CSV modules.
0
 

Author Comment

by:sud
ID: 17981780
Hi Experts,
        my first row is a column name, so how to take care of that?

thanks,

sudhir
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 50 total points
ID: 17982028
put this before the while loop:
my @columnnames=split(/,/,<CSV>);
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sud
ID: 18021886
Hi Experts,

    I am able to read .CSV file,

   Now I am trying to compare value of first column from .csv file to file name in the directory(A:/temp/RAW), if it matches then print the filename.

        Can you please help me how to do it.

here is my code

use strict;
use warnings;
use File::Find;

open CSV, "A:/temp/test.csv" or die "Can not open file.csv $!\n";

my @columnnames=split(/,/,<CSV>);

while (<CSV>) {
  chomp;
  my ($list1, $list2, $list3) = split /,/;


 find (\&matchPattern, 'A:/temp/Raw');

sub matchPattern
{
        if ($File::Find::name =~ '.txt' )  {
     if ($File::Find::name = "$list1"){
 
         print "$list1";
  }  
   
   
 }

 }

}


I would appreciate ur help or suggestion. Thanks,

0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 18022489
use strict;
use warnings;
use File::Find;

open CSV, "A:/temp/test.csv" or die "Can not open file.csv $!\n";

my @columnnames=split(/,/,<CSV>);
my %list;
while( <CSV> ){
  my $list = (split /,/)[0];
  $list{$list}++ if $list =~ /\.txt/;
}

local $\="\n";
 find (\&matchPattern, 'A:/temp/Raw');

sub matchPattern
{
       print if $list{$File::Find::name}
}
0
 

Author Comment

by:sud
ID: 18022652
Hi Ozo,
    Somehow ur program does not give any output.

My following program gives me correct output except that it prints result twice , any suggestion ?

use strict;
use warnings;
use File::Find;
find (\&matchPattern, ''A:/temp/Raw'');

sub matchPattern
{
       if ($File::Find::name =~ '.xpt'){
      open CSV, "A:/temp/test.csv" or die "Can not open file.csv $!\n";

      my @columnnames=split(/,/,<CSV>);

      while (<CSV>) {
     chomp;
    my ($list1, $list2, $list3) = split /,/;
      if ($File::Find::name = "$list1"){

     print "$list1";
                     }
              }  
       }
}
0
 
LVL 9

Assisted Solution

by:Suhas .
Suhas . earned 50 total points
ID: 18060315
Hi Sudhir,

Make sure your test.csv file dont have two files.
because while loop reads data from that file and prints the $list1 after doing split.

Cheers...
Suhas
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

21 Experts available now in Live!

Get 1:1 Help Now