Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Answer: How do I pull n characters off the front of a string?

by perlmonkey (Hermit)
on May 02, 2001 at 11:48 UTC ( #77258=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I pull n characters off the front of a string? - Answer contributed by perlmonkey

Use substr when possible, it can be many times faster then a simple regex:
my $a = "abcdefghijklmnopqrstuvwxyz"; use Benchmark; timethese(1000000, { 's1' => sub { s1($a, 5) }, 's2' => sub { s2($a, 5) }, }); sub s1 { my $a = $_[0]; $a =~ s/^.{$_[1]}//; return $a; } sub s2 { substr($_[0], $_[1]); } Results: s1: 8 wallclock secs ( 7.95 usr + 0.09 sys = 8.04 CPU) s2: 2 wallclock secs ( 2.54 usr + 0.01 sys = 2.55 CPU)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[erix]: next time, drop by :)
[LanX]: religion is also the reason how the house of Hanover aquire the English throne, all other candidates on succession where catholics...
[erix]: Henry VIII has a lot to answer for :P
[LanX]: erix sure next time you miss a conference nearby we drop by ...
[LanX]: did you even go to the Dutch Perl Workshop in Utrecht?
[erix]: haha ok. I'm going to Glasgow though, and make a long road trip round the isles
[erix]: [Lanx} not the last time but the one before (there have been two)
[LanX]: when did you move to Utrecht, you used to live in a windmill on a dyke walking around in wooden shoes. ..
[LanX]: ... did you lie to me? OO
erix wouldn't mind a nice old windmill to live in... Mueller/Miller/ Molenaar

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-12-15 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (439 votes). Check out past polls.

    Notices?