Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Efficient walk/iterate along a string

by moritz (Cardinal)
on Nov 23, 2010 at 12:54 UTC ( #873199=note: print w/ replies, xml ) Need Help??


in reply to Re: Efficient walk/iterate along a string
in thread Efficient walk/iterate along a string

Just a quick warning: if the string contains 0s, the chop solution stops as soon as one is found. Also chop does not set $_ to the removed character.

A version that fixes both problems, and is still faster than unpack is

my $s = reverse $string; my $c; $c = chop $s while length $s;


Comment on Re^2: Efficient walk/iterate along a string
Download Code
Re^3: Efficient walk/iterate along a string
by BrowserUk (Pope) on Nov 23, 2010 at 13:29 UTC

    True, but the extra opcode (length) imposes a 30% hit.

    Of course that diminishes if you're doing anything useful within the loop, but it is still significant for those situations where using such an obscure mechanism is worth considering.

    Luckily, genomic data doesn't usually contain zeros or nulls.

    I think it would be really nice if in the same way that in 5.12 you can use each on arrays, it would be nice to use it on scalars:

    my $string = 'fred'; my( $i, $c ); say "$i:$c" while ($i,$c) = each $string; 0:f 1:r 2:e 3:d say while $_ = each $string; f r e d

    I think that could be made very efficient by aliasing a LvTARG to the characters in situ; and would be very useful.

    IMO far more useful than the single character saving of each $arrayRef; over each @$arrayRef, which unfortunately probably means that it could not now be implemented :(


    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://873199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-07-23 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (137 votes), past polls