in reply to Re^3: Finding word either side of a word match
in thread Finding word either side of a word match

That's a far more elegant solution :)
What would be the best way of finding the position in bytes? Its not something that I've come across yet.
  • Comment on Re^4: Finding word either side of a word match

Replies are listed 'Best First'.
Re^5: Finding word either side of a word match
by moritz (Cardinal) on Mar 04, 2008 at 08:08 UTC
    You can slurp the whole file into meory like this:
    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.