I copied your subroutine SplitCSVLine into a different file and put the following before it

#!/usr/bin/perl use strict; use warnings; my @res= SplitCSVLine('123,hubba hubba,bla fasel,hell'); print ':',join":",@res,":\n";

When I started the program, the result was:

:123:hubba hubba:bla fasel:hell::

This suggests two things: 1) For some input values it works without getting into an endless loop. 2) It returns an empty field after the last (i.e. the :: after 'hell' is an empty string), so it probably isn't working completely as intented.

After looking at the regular expressions I also tried the string '123,"hubba" ,bla fasel,help' and this got into an endless loop. The problem is that the second s/// regex doesn't allow any spaces between a string in double quotes and the following comma. This could be fixed with changing that regexp to

$line =~ s/\"((\"\"|[^\"])*)\"\s*(,|$ )//x;

But is that all or did I overlook another case that leads to an endless loop? I don't know. A much better way to deal with this bug would be to substitute your subroutine with a module like Text::CSV that is much better tested and probably takes care of anything that could be thrown at it.

By the way, it would have been much easier for you to find the problematic case than it was for me to analyze the subroutine. Just print out any values delivered to the subroutine (i.e. insert print "|$line|\n"; after the first line inside the subroutine). The last value you see printed should be the one leading to the endless loop

You could even add print statements inside the while loop to see how $line changes and ultimately how it doesn't change anymore.


In reply to Re^4: Script works in XP but not Ubuntu? by jethro
in thread Script works in XP but not Ubuntu? by BassKozz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.