in reply to Chopping the beginning of a string?

Seems to me that
$str = substr($str,1);
is about as fast, straight-forward, and easy to understand as i need. I would only use reverse if i needed to reverse the string ... but this is better used in conjunction with a regex (see sexeger).

If you would like to see a more obscure way, how about using split and join? (don't try this at work)

$str = join('',(split('',$str))[1..length($str)]);

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Chopping the beginning of a string? (4-arg)
by tye (Sage) on Oct 29, 2003 at 16:53 UTC

    I prefer the 4-arg substr for this case:

    my $firstChar= substr($string,0,1,"");
    Because:
    • It avoids copying of the string (it simply updates an offset to where the start of the string is -- for more fun with this trick see (tye)Re: Creative use of pack/unpack) -- some day Perl might be smart enough to optimize your code to act the same as my code, but it isn't that smart today
    • It lets you save the removed character in a single step

                    - tye