Solved

Reading Excel File

Posted on 2006-11-19
8
139 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

20 Experts available now in Live!

Get 1:1 Help Now