in reply to character offset to word offset
There are a couple of things to think about here. You need to figure out how you are going to store the word spans (where do they come from btw?). Deciding just what is a word can be tricky (e.g. for example).
When editing involves changing the length of whatever is being edited it is often better to work backwards so that you don't change the indexes for subsequent edits. The following somewhat golfed code (don't hand it in to your teacher) counts words by counting the spans of spaces between them:
use strict; use warnings; my $sentence = 'Sam goes to school to play football.'; my @subs = ([12, 17], [27, 34]); my @counts; @subs = sort {$b->[0] <=> $a->[0]} @subs; # Descending sort by first c +har pos for my $sub (@subs) { unshift @counts, 1 + (substr $sentence, 0, $sub->[0]) =~ s/(\s+)/$ +1/g; substr $sentence, $_, 0, '**' for $sub->[1] + 1, $sub->[0]; } print "@counts $sentence\n";
Prints:
4 7 Sam goes to **school** to play **football**.
|
|---|