Solved

Reading Excel File

Posted on 2006-11-19
8
144 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Assisted Solution

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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 …
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

628 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