in reply to character offset to word offset
I'm not sure I understand correctly what you want to do, but I'll try:
my $sentence = 'Sam goes to school to play football.'; substr($sentence, $_, 0) = '**' for 35, 27, 18, 12; print $sentence, "\n";
This will print Sam goes to **school** to play **football**., and the asterisks have been put there using the character offsets.
However, a more straightforward way to highlight school this way would be something like:
$sentence =~ s/(school)/**\1**/;
Update: Sorry, I forgot the part about the word count.. Well, here's a way to get to the "4" (not very nice, maybe others have a more beautiful solution):
my $sentence = 'Sam goes to school to play football.'; # Count the spaces and add 1 my $count1 = substr($sentence, 0, 12) =~ y/ // + 1;
|
|---|