Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: character offset to word offset

by ww (Archbishop)
on Sep 08, 2008 at 23:04 UTC ( [id://709908]=note: print w/replies, xml ) Need Help??


in reply to character offset to word offset

-- for failure to show even a hint that you've tried anything other than an appeal to others... and another appears likely to be deserved for homework not labeled as such.

Please see How do I post a question effectively? -- specifically, RTFM (Show Some Effort), Do Your Own Work, and About Homework.

Replies are listed 'Best First'.
Re^2: character offset to word offset
by AnomalousMonk (Archbishop) on Sep 09, 2008 at 00:08 UTC
    I share the suspicions of and disdain for homework not labeled as such expressed by others, but since several code contributions have already been made, here's mine.

    As noted above, the tricky part is defining the word regex. This should also probably be defined separately and passed to the emphasize() function rather than being hard-coded.

    perl -wMstrict -le "{ my %emphasis; my $word = qr{ (\b \w+ \b) }xms; sub emphasize { my $string = shift; %emphasis = map { $_ => 0 } @_; my $intro = qq{@{[ sort { $a <=> $b } keys %emphasis ]}}; my $words = 0; $string =~ s{ ($word) } { exists $emphasis{++$words} ? qq{**$1**} : $1 }xmsge; return qq{$intro $string}; } } my $string = 'Sam goes to school to play football.'; print emphasize($string, 4, 7); print emphasize($string, 7, 2, 4); print emphasize($string); print emphasize('the cow jumped over the', 3, 4, 1); " 4 7 Sam goes to **school** to play **football**. 2 4 7 Sam **goes** to **school** to play **football**. Sam goes to school to play football. 1 3 4 **the** cow **jumped** **over** the

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2026-03-17 03:24 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.