Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Extracting the number of %'s in a string

by Zaxo (Archbishop)
on Dec 13, 2007 at 09:46 UTC ( #656782=note: print w/replies, xml ) Need Help??

in reply to Extracting the number of %'s in a string

To count them,

$_=q(aaa%bbbbb%cccccc%ddd); print tr/%//, $/;
prints '3'

To extract their positions;

$_ = 'aaa%bbbbb%cccccc%ddd'; print $-[0], ' ' while /%/g; print $/'
prints "3 9 16".

See perlvar for the @- array magic. You would push those values to an array, instead of printing, for further use.

After Compline,

Replies are listed 'Best First'.
Re^2: Extracting the number of %'s in a string
by moritz (Cardinal) on Dec 13, 2007 at 10:08 UTC
    The counting solution will give a wrong result if the string has a trailing %, because the OP wants the number of %s followed by another character.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2019-05-21 02:31 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (129 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!