Solved

Reading Excel File

Posted on 2006-11-19
8
140 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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.…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

778 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