On Wed, 10 Dec 2003, [EMAIL PROTECTED] wrote:

On Dec 10, 2003, at 9:39 PM, Ron Newman wrote:
> 
>> If I slurp a file into a string variable (either Uri's way or my
>> own way), and then do a regexp match, I'd like to determine the
>> line number(s) of the match.  Is there a straightforward or
>> canonical way to do this?
> 
> I've already gotten one private reply of "don't slurp the file".  To
> head off any more, I should add that my regexp match may span
> multiple lines.

Off the top of my head, highly unoptimized suggestions:

1) if you know the maximum number of lines your regexp may match,
   keep a revolving buffer of that many lines instead of loading the
   whole file.

2) perldoc -f pos, then count the newlines up to the position

$a = "hello\ntzz"; 
$a =~ m/tzz/g; 
$pos = pos($a); 
$upto = substr($a,0,$pos);
$count = $upto =~ tr/\n//; 
print "Position is [$pos], newlines [$count] in string [$upto]\n";

3) Consider alternative approaches.  For instance, maybe reading the
   file backwards (see File::ReadBackwards) will make your match
   easier.  Maybe, if the regexp can match only once, you can start
   from the beginning of the file and from the end of it at the same
   time, and keep reading on both sides (front, check, back, check,
   etc.) until you either match or meet in the middle.  For large
   files this may be much more efficient.

Ted
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to