I had a working solution to your problem yesterday, in
this node. It got hidden by the depth threshold. I've included it below.
As I said earlier, you can't possibly use 'undef $/' and expect to get line numbers, since with undef $/, everything is read as a single line.
There are certainly more elegant solutions than mine, and many are probably already posted in this thread. But, this is a pattern I've used repeatedly in my own code.
I found a working solution, though incomplete. I'm not sure what the last few lines of your regex do. I'm guessing it also finds all quoted strings in the text. You can modify this to work for that case too.
my $match;
my $line;
while(<F>) {
if(m{/\*} .. m{\*/}) {
## single line
if(m{(/\*.*?\*/)}) {
$match = $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
} else {
## multi-line
if( m{(/\*.*)} ) {
## Initial line '/*'
$match = $1 . "\n";
$line = $.; # record this line number
} elsif( m{(.*\*/)} ) {
## Final line '*/'
$match .= $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
$match = undef;
$line = undef;
} else {
# We are between lines, and have no /* or */
$match .= $_;
}
}
} elsif ( m{(//.*)\Z} ) {
$match = $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
}
}
So, this stores the starting line number and comment string in a hash.
Hopefully this gives you an idea of how to process several lines. It's certainly not as nice as the quick and simple grep solution, but grep aggregates all the results together, so you can't get line numbers out of the results.
I did see this in the output of perldoc -f grep
'grep returns aliases into the original list'
so, perhaps you can somehow map back the results into the original array, but I have no idea how.
My ideal solution would be some variant of the grep statement you have, perhaps with a map statement instead of grep, and some nice (linenumber, string) pair returned.
Can't seem to find anything that works though.
Hope this helps,
~J
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.