perl function enquery


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl function enquery
# 1  
Old 04-05-2012
perl function enquery

Dear all,

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

Code:
.....
    @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;
}
............................

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them
# 2  
Old 04-05-2012
Hi eldonlck,

It inserts in @new each field of a CSV file.
# 3  
Old 04-06-2012
Dear all,

Does any body knows what the following pattern matching means ?
Code:
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,


Last edited by Franklin52; 04-08-2012 at 06:31 AM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 04-06-2012
"([^\"\\]*(?:\\.[^\"\\]*)*)",? => 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
# 5  
Old 04-06-2012
what is purpose of (?: (?=\\.[^\"\\]*))* for the code above
# 6  
Old 04-06-2012
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.
# 7  
Old 04-06-2012
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl qr function usage

I'm not sure why I'm having so much trouble with this. I think I'm really not understanding how this works. I'm trying to store a regex in a variable for use later in a script. Can someone tell me why this doesn't match??? #!/usr/bin/perl # # # $ticket=1212; my $rx_ticket =... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Perl split function

my @d =split('\|', $_); west|ACH|3|Y|LuV|N||N|| Qt|UWST|57|Y|LSV|Y|Bng|N|KT| It Returns d as 8 for First Line, and 9 as for Second Line . I want to Process Both the Files, How to Handle It. (3 Replies)
Discussion started by: vishwakar
3 Replies

4. Shell Programming and Scripting

How to use parameter with perl function?

am a beginer of shell Unix, plesase tell me how to get parameter with $perl in loop $ perl -lne '$/="DOCEND";print $_."DOCEND" if /$ACC/' file_input > output i can't get parameter in loop with perl fucntion like this pass paramerter to $ACC not accept in this script $ACC = paramerter to... (4 Replies)
Discussion started by: krai
4 Replies

5. Shell Programming and Scripting

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

6. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

7. UNIX for Dummies Questions & Answers

perl bless function

hi i am not getting what exactly bless function do in perl explanation in perldoc is not very clear i tried to search on google but i am getting confused or rather not getting at all. can anybody explain in short what it does in following example as well as in general ? sub new { my... (1 Reply)
Discussion started by: zedex
1 Replies

8. Shell Programming and Scripting

sort function in perl

Hi, here is my perl script.This script creates an array and is sorting it using the in-built sort function in perl. #!/usr/local/bin/perl my number=6; my @num_arr=(1,2,3,4,5); my @array=(23,"$number","Hello",2.345,@num_arr); #printing the array print... (2 Replies)
Discussion started by: DILEEP410
2 Replies

9. Shell Programming and Scripting

perl split function

$mystring = "name:blk:house::"; print "$mystring\n"; @s_format = split(/:/, $mystring); for ($i=0; $i <= $#s_format; $i++) { print "index is $i,field is $s_format"; print "\n"; } $size = $#s_format + 1; print "total size of array is $size\n"; i am expecting my size to be 5, why is it... (5 Replies)
Discussion started by: new2ss
5 Replies

10. Programming

Memory Deallocation Enquery

If i "new" a object of class type A and A uses some data structure like array, set, map etc. When i delete this object, need I delete/clear the array/set/map in the destrucator of A, or system will deallocate the memory automatically?? Thanks in advance! Em, do bear me if the question is... (3 Replies)
Discussion started by: zzz_zzz
3 Replies
Login or Register to Ask a Question