Assuming that you want to retain the comment regions in the resulting array -- and assuming that the code does not pose any nasty challenges (e.g. quoted strings that contain just "/*" or just "*/", and similar perversions) -- something like this might get you started:
my $code; { local $/; $code = <DATA>; } my @pieces = split m{/\*|\*/} $code; # split on comment delimiters; my $incomment = 0; my @csv = ( '' ); for ( @pieces ) { if ( m{/\*} ) { $incomment = 1; $csv[$#csv] .= $_; } elsif ( m{\*/} ) { $incomment = 0; $csv[$#csv] .= $_; } elsif ( $incomment ) { $csv[$#csv] .= $_; } else { my ( $first, @rest ) = split /,/, $_, -1; # don't truncate tra +iling commas $csv[$#csv] .= $first; push @csv, @rest if ( @rest ); } }
(not tested)

But the more I think about it, the more likely it seems that you really just want to delete comments first, and split whatever is left on commas. In which case, you should probably still slurp the whole input, in case comments are allowed to span multiple lines:

{ local $/; $code = <>; } $code =~ s{ */\*.*?\*/ *}{ }gs; # allow "." to match "\n" # now split on commas (and newlines?)
(also not tested)

If you're still struggling, it would be good to show us some sample input, what the resulting array should hold, and any real code you've actually tried so far. Your initial problem statement was not very detailed.


In reply to Re: Splitting outside comments by graff
in thread Splitting outside comments by Sprad

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.