Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: The nth occurrence of a character

by mickeyn (Priest)
on Feb 08, 2006 at 15:03 UTC ( #528813=note: print w/replies, xml ) Need Help??

in reply to The nth occurrence of a character

allow me to suggest another way that may serve you if you want to access different characters at different occurances many times:

my %hash; my @split = split //, $string; my @positions = map { [$split[$_], $_] } 1 .. @split-1; undef @split; foreach (@positions){ push @{$hash{$_->[0]}}, $_->[1]; } undef @positions ;

now, for each character you have a hash entry with all its positions in the string (array).
if for example you want to get the 3rd occurance of 'A', you will simply need to access: $hash{A}[2]
(2 will be the 3rd index in the array)

You wannted efficient function, and this way allows you to get you data in one hash+array access which IMHO is efficient.
the price you will pay is the one-time building of the hash (per string).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://528813]
[choroba]: that's 2012
[zentara]: I just acquired a computer with the card type, and wondered if it was best to stick with the open-source code or use NVIDIA's proprietary code
[zentara]: do you feel good about a non-open-source code, being in control of your display functions?
[choroba]: If you're not going to play 3d games on it, using the open source driver should be enough
[zentara]: choroba that is true, and the wise choice, but I want to see how fast I can get fglrx3dspheres to run... just as an intellectual challenge :-)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-05-26 14:02 GMT
Find Nodes?
    Voting Booth?