powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to build a script that will parse a file that people filled out for more information about a clients product.

Some of these people put like numbers like, 5,000 and 1,000 and they are comma delimited fields, thus that entry will throw off the rest of them. Usually when they are delivered to me those fields are all surrounded by "".

So I did this:
$_lfile = param("lfile"); # Get the contents via copy n paste $_lfile2 = $_lfile; # Make a temp variable ($_line1,$lfile2) = split /\015\012/, $_lfile2, 2; # Split by line bre +aks in windows format while($_line1 =~ /\"/) { # Check if this line contains quote ($_line1,$_extraLine1) = split /\"/, $_line1, 2;# Split by Quote if($_extraLine1 =~ /\"/) {#If contains another then spolit again ($_extraLine1,$_extraLine2) = split /\"/, $_extraLine1, 2; }# End the check to see if contained another quote. $_extraLine1 =~ s/\,/g;#Now strip out the comma's in the field bet +ween the quotes. $_line1 = "$_line1$_extraLine1$_extraLine2";# Now replace the line + contents, without the quotes and extra comma's }# End while statement
Now I get this error:

syntax error at /home/path_to_files/pages/file_parser.conf line 29, near "while(($__field,$_line1) = split /\" Compilation failed in require at index.cgi line 39.

That is the code just under the above. If I comment the above code out then there are no errors in that part of the code, it works just fine. Can I not do a contains "=~" looking for a quote? I tried it escaped and not escaped, same result.

Thanks for any wisdom...

thx,
Richard

Replies are listed 'Best First'.
Re: Checking if line contains a quote
by reneeb (Chaplain) on Sep 08, 2005 at 07:51 UTC
    In this line $_extraLine1 =~ s/\,/g; is a mistake. It should be $_extraLine1 =~ s/\,//g;
      or $_extraLine1 =~ tr/,//d;
Re: Checking if line contains a quote
by salva (Canon) on Sep 08, 2005 at 08:21 UTC
Re: Checking if line contains a quote
by liverpole (Monsignor) on Sep 08, 2005 at 11:07 UTC
    I agree with reneeb that you need the extra '/' in the global substitution. However, there's no reason for the backslashes in the regular expressions.  Neither quotes (") nor commas (,) are regular-expression metacharacters, so just write them like this:
    while($_line1 =~ /"/) { ... $_extraLine1 =~ s/,//g;
    Is there some reason you're not using "use strict" and "use warnings"?  They're very useful, and would point out the locations where you've neglected to create lexically-scoped variables with my.  You may also want to rethink the use of leading '_' in your variable names, which typically have the connotation of being a program-internal variable.  And finally, do you really want the clutter of comments like:
    }# End the check to see if contained another quote.
    and
    }# End while statement
    in the code?   It's pretty easy to see where the opening '{' for each of them are; all the more so if you indent prudently and don't add unhelpful comments to the mix.