Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: how do you use the index function on this example?

by Anonymous Monk
on Sep 21, 2015 at 19:48 UTC ( [id://1142665]=note: print w/replies, xml ) Need Help??


in reply to how do you use the index function on this example?

Ah, never mind, I found this solution:
while($string=~/(M+)/g) { ($start, $end) = ($-[0], $+[0]); print $start."\t".$end."\n"; }

Thanks

Replies are listed 'Best First'.
Re^2: how do you use the index function on this example?
by AnomalousMonk (Archbishop) on Sep 21, 2015 at 22:12 UTC

    And you don't even need the capturing parentheses:

    c:\@Work\Perl>perl -wMstrict -le "my $string = 'IIIIMMMMMMMOOOOOOOOOOMMMMMMMMMMMMMMMIIIIIIMMMMOOOO'; ;; while ($string =~ /M+/g) { my ($start, $end) = ($-[0], $+[0]); print qq{start $start, end $end}; } " start 4, end 11 start 21, end 36 start 42, end 46
    Note that  @+ holds the positions of the first character after matched sub-strings!


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found