Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: character offset to word offset

by mr_mischief (Monsignor)
on Sep 08, 2008 at 20:50 UTC ( [id://709871]=note: print w/replies, xml ) Need Help??


in reply to character offset to word offset

Getting the words from the character offsets is easy enough with substr. Here's an easily adaptable way to count the words before a particular word in a string for a sufficiently simple definition of 'word':

sub words_before { my ( $word, $string ) = @_; my ( $before ) = $string =~ m/(.*?)\Q$word\E/g; my $words_before = () = $before =~ m/(\S+)/g; return $words_before; }
This can be used such as this:
print 'There are ' . words_before( $word, $string ) . ' words before " +' . $word . '" in the string "' . $string . '"' . "\n";
The word index counting from 0 is the same as the number of words before the given word. Counting from 1, you'll need to add one to that amount.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://709871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2026-03-16 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.