Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: finding the position of a particular character in a string

by keszler (Priest)
on Oct 06, 2011 at 03:29 UTC ( #929920=note: print w/replies, xml ) Need Help??


in reply to finding the position of a particular character in a string

perl -E' $a="THE FOOD IS DELICIOUS"; $srch="O"; push @i,index($a,$srch,$i[-1]+1) for $a=~/$srch/g; $" = ","; say "@i"; ' 5,6,18

Replies are listed 'Best First'.
Re^2: finding the position of a particular character in a string
by BrowserUk (Pope) on Oct 06, 2011 at 04:45 UTC

    Searching twice is expensive and unnecessary:

    $a="THE FOOD IS DELICIOUS";; $p=0; @p=(); push @p, $p-1 while $p = 1+index $a, 'O', $p; print @p;; 5 6 18

    Or:

    $a="THE FOOD IS DELICIOUS";; @p=(); push @p, $-[0] while $a =~ m[O]g;; print @p;; 5 6 18

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929920]
help
Chatterbox?
[LanX]: you can manipulate a html tree by addressing and replacing so called document objects (DOM) and from my experience the same methods can be used with any XML, though I didn't test all browsers yet
LanX just joking, playing "ego judo"
[marto]: good morning all
[Corion]: Hi marto!
[marto]: the race to the bottom continues, many people lost their job yesterday as a client moves to another outsourcer
[LanX]: brexit or perlxit ?
[Corion]: Ow, marto :-/ I hope you aren't affected (except for the looming dread of more work for the same pay)
[marto]: unrelated
[marto]: Corion, no we're already being g tupe transferred to another employer. This was a banking sector client. They provided a reporting platform using ancient tech

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2017-03-23 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.