Beginner wrote:
On 3 Sep 2007 at 16:12, Andrew Curry wrote:

Please do not attribute to Andrew Curry a post that was actually submitted by me (see my name at the end there.) TIA


$ perl -le'
$_ = q[SPEED OF LIGHT, ,  LIGHT SPEED,TRAVEL,TRAVELLING, ,
DANGER,DANGEROUS,PHYSICAL, ,  CONCEPT,CONCEPTS, , , , , , , , , , ];  >
print; s/,\s*(?=,)//g; print; '
SPEED OF LIGHT, ,  LIGHT SPEED,TRAVEL,TRAVELLING, ,
DANGER,DANGEROUS,PHYSICAL, ,  CONCEPT,CONCEPTS, , , , , , , , , , SPEED OF
LIGHT,  LIGHT SPEED,TRAVEL,TRAVELLING,  DANGER,DANGEROUS,PHYSICAL,
CONCEPT,CONCEPTS,


$ perl -le'
$_ = q[SPEED OF LIGHT, ,  LIGHT SPEED,TRAVEL,TRAVELLING, ,
DANGER,DANGEROUS,PHYSICAL, ,  CONCEPT,CONCEPTS, , , , , , , , , , ]; print;
$_ = join ",", grep /\S/, split /,/; print; '
SPEED OF LIGHT, ,  LIGHT SPEED,TRAVEL,TRAVELLING, ,
DANGER,DANGEROUS,PHYSICAL, ,  CONCEPT,CONCEPTS, , , , , , , , , , SPEED OF
LIGHT,  LIGHT SPEED,TRAVEL,TRAVELLING,  DANGER,DANGEROUS,PHYSICAL,
CONCEPT,CONCEPTS




John

Okay I need to ask what's going on here. I had to use the s/,\s*(?=,)//g expression because the s/(\,+\s*)+/,/g; regex in my code snip wasn't working as it did on the text snippet I originally supplied.

"wasn't working" is not a very good description of the problem.


=== code snip ===
 while (<FH>) {   
        chomp($_);      

Why remove the newline and then add it back at the end of the loop?

        s/"//g;    

It is more efficient to use transliteration to remove characters from a string:

        tr/"//d;

        s/\t/, /g;      
        s/,\s*(?=,)//g;         
print "\"$_\"\n";

You could use different quoting so you don't have to escape the quotation marks:

         print qq["$_"\n];

}
========== I can understand the 2nd method: A grouped, literal comma (\,), one or more times followed by a zero or more spaces. The 2nd regex reads to me like, a comma then zero or more spaces but what's that (?=,) doing?

It is a zero-width positive look-ahead assertion. It says that a comma *must* follow the pattern but is not included as part of the pattern.


Is it referring to the preceding expression and saying if it matches up to 1 time? I can't see what the equal sign is doing either.

Enlightment please.

perldoc perlre




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to