Solved

Reading Excel File

Posted on 2006-11-19
8
143 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

738 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