perl function enquery

Dear all,

I find a perl script that contains the following codes. Does anybody know the meaning of codes highlight.

.....
    @field = parse_csv($file);
    chomp(@field);
........
........
sub parse_csv {
    my $text = shift;
    my @new  = ();
    push( @new, $+ ) while $text =~ m{
       "([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,
       }gx;
    push( @new, undef ) if substr( $text, -1, 1 ) eq ',';
    return @new;
}
............................

Hi eldonlck,

It inserts in @new each field of a CSV file.

Dear all,

Does any body knows what the following pattern matching means ?

"([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,

"([^\"\\]*(?:\\.[^\"\\]*)*)",? => Are you sure this is it? Looks like something is missing after (?: I think it could be (?: (?=\\.[^\"\\]*))

"([^\"\\]*(?: (?=\\.[^\"\\]*))*)",? --> Looks for double-quote, not followed by a double quote or \ which precedes a \, any character and more characters which are not a double quote or \. This pattern (?: (?=\\.[^\"\\]*)) would not be a part of match. And finally match a comma (,? indicates one or no comma)

        ``` |  ([^,]+),? ```  -->  OR match many characters that is not a comma followed by a comma \(,? indicates it can exist once or not\)

| , --> OR match a comma

what is purpose of (?: (?=\\.[^\"\\]*))* for the code above

Its like a conditional matching. For e.g., you want to match 'a' that comes strictly before 'b', but don't want 'b' to be a part of your match.

Your pattern would be something like this: /a(?=b)/ , this would match string 'abc' but not 'adc' or 'dac'.

It's a bit confusing, I know. But this is the way it works. There're work-arounds available. With perl, there's always more than one way to do it. If you could post your requirement and desired output, members of this forum would give it a shot.

Hi balajesuri , Thank you for your reply,

Actually my requirement is that I want to find a perl scripts which can read a CSV file
with the format
"xxxx","xxxx","xxxx","xxxxx"

and then write them into the first worksheet of an excel file which contains some formula in the other worksheet. Some data analysis would be done based on the data of the first worksheet.

I don't know if it could be done or not in perl. First of all, I would like to find some perl script to read CSV file first , I found the code above on the net how to read CSV file with pattern matching although I do not fully understand what that means
Secondly I want to find any perl script how to write them into excel file

See anybody can help me to address my issue?

could this help you ?

#!/usr/bin/perl
use strict;
use Spreadsheet::WriteExcel;
my $sheet;
my ($row,$col)=(0,0);
my @newflds;
my $j=0;
my $source_csv = shift @ARGV  or die "invocation: $0 <Source CSV> <destination file>";
my $destname = shift @ARGV or die "invocation: $0 <Source CSV> <destination file>";
my $dest_book  = Spreadsheet::WriteExcel->new("$destname") or die "Could not create a new Excel file in $destname: $!";
my $dest_sheet = $dest_book->addworksheet($sheet);
open (FH,"<","$source_csv") or die "Can not open csv file \n $! \n";
while (<FH>) {
        chomp;
        @newflds=split(/,/);
        foreach (@newflds) {
                $dest_sheet->write($row,$col++,$_);
        }
        $row++;
        $col=0;
}
$dest_book->close();
close(FH);
print "done!\n";

invocation:

 script <Source CSV> <destination file>" 

HI pravin27

Since I am newbie in perl, I find that the code above includes
the package
use Spreadsheet::WriteExcel;
As far as I know, the package should be downloaded. How can I download this package for Solaris 10 unix and then apply it to unix box?

Spreadsheet::WriteExcel - search.cpan.org

Also, the same website contains details on how to install packages.