Re: [MacPerl-AnyPerl] slurp - line numbering

[email protected] (Bart Lateur) Sun, 14 Oct 2001 18:26:50 +0200
Newsgroups perl.macperl.anyperl
Organization MediaMind
Message-ID <[email protected]>
On Sun, 14 Oct 2001 13:00:15 +0200, allan wrote:

>is it somehow possible - while slurping a file - to still keep track of
>the line numbers?

>if ($txt =~ /55$/m) {
>	print  $.;
>	# should print 3
>}

You can use pos() with the /g modifier, to find the offset of where the
substring ends. Then you'll have to count the newlines in front of it.

	if($txt =~ /55/g) {
	     print 1+ substr($txt, 0, pos($txt))=~tr/\n//;
	}

(BTW you won't get a match if your lines look exactly like that.)

-- 
	Bart.