in reply to Regex Strikes again!

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

Replies are listed 'Best First'.
Re: Re: Regex Strikes again!
by nofernandes (Beadle) on Jul 17, 2003 at 08:50 UTC

    Thank you very much once again!!

    You all have been very helpfull!!

    Your advices help me a lot!!

    Thanks

    Nuno