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

why won't this work?
#!/usr/bin/perl use warnings; use strict; my $should_be = <<"END_SHOULD_BE"; input is: , , 1 , , , 2 , , , , 3 , , , , , 4 , , , , , , 5 , , , , , , , 6 , , , , , , , , 7 , , , , , , , , , 8 , , , , , , , , , , 9 , , , , , , , , , , , 10 output should be: ,, 1 ,,, 2 ,,,, 3 ,,,,, 4 ,,,,,, 5 ,,,,,,, 6 ,,,,,,,, 7 ,,,,,,,,, 8 ,,,,,,,,,, 9 ,,,,,,,,,,, 10 output is: END_SHOULD_BE print "$should_be"; while (<DATA>) { s{, +,}{,,}g; print $_; } __DATA__ , , 1 , , , 2 , , , , 3 , , , , , 4 , , , , , , 5 , , , , , , , 6 , , , , , , , , 7 , , , , , , , , , 8 , , , , , , , , , , 9 , , , , , , , , , , , 10

Replies are listed 'Best First'.
Re: remove spaces only between commas
by ikegami (Patriarch) on Sep 21, 2007 at 17:15 UTC

    The first match includes the second comma, so the second match will start at the third one.

    Before s///: , , , , , , , 6 After 1st pass: ,,| , , , , , 6 After 2nd pass: ,, ,,| , , , 6 After 3rd pass: ,, ,, ,,| , 6

    The | indicates where a substition ended and from where the next pass will search.

    Solution 1: Start from the begining instead of where the last match left off.

    1 while s{, +,}{,,};

    Solution 2: Use a zero-width match to check for the "closing" comma.

    s{, +(?=,)}{,}g;
      thanks ikegami. solution 2 is perfect.