in reply to Re^4: Finding word either side of a word match
in thread Finding word either side of a word match
open (my $handle, '<', $file) or die "Can't read '$file': $!"; my $contents = do { local $/; <$file> };
And then when you match against that string, you can query pos $contents to get the position of the match, which is the same as the position in bytes. (Note that you will run into troubles with multi byte encodings this way).
Another way is to read the file line by line, and track the number of characters that have been consumed so far:
my $pos = 0; while (<$handle>){ my $line_len = length $_; # do that before chomping chomp; while (m/(\w+)/g){ my $word = $1; my $word_pos = $pos + pos; } $pos += $line_len; }
See pos.
|
|---|