in reply to Splitting outside comments
(not tested)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 ); } }
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:
(also not tested){ local $/; $code = <>; } $code =~ s{ */\*.*?\*/ *}{ }gs; # allow "." to match "\n" # now split on commas (and newlines?)
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Splitting outside comments
by Sprad (Hermit) on May 04, 2006 at 15:48 UTC |