Contributed by Anonymous Monk
on Apr 24, 2001 at 20:03 UTC
Q&A
> strings
Description: If I have a string that I am parsing to put into a database and the same n characters are being pulled off a string, I want to pull those chars from the string and leave the rest of the string. I want the first n chars put into $x, then removed from the string. Then the first n chars in the new string put into $y. Answer: How do I pull n characters off the front of a string? contributed by arturo Bah! Why use regexes when there's a faster function designed for the purpose? =)
#assuming $n contains the number ...
my $first_n = substr($string, 0, $n);
#to grab $n off the end
my $last_n = substr($string, -1, $n);
| Answer: How do I pull n characters off the front of a string? 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)
| Answer: How do I pull n characters off the front of a string? contributed by davido The other examples all handed off the first $n characters. But they didn't remove those first $n characters from the original $string, as the question required.
As always, there is more than one way to do it. For each of the examples below, assume the following setup:
my $string = "1234567890abcdefghijABCDEFGHIJK";
my $chars;
my $n = 2;
With substr:
$chars = substr( $string, 0, $n );
substr( $string, 0, $n ) = "";
Another with substr:
( $chars, $string ) = ( substr($string,0,$n), substr($string,$n) );
And probaby the best substr solution:
$chars = substr ($string, 0, $n, "");
With a substitution s/// regexp:
$string = s/^(.{$n})(.*)$/$2/s;
$chars = $1;
With split:
($chars, $string) = split /^(.{$n})/, $string, 1;
With a pattern match (m//):
($chars, $string) = $string =~ /^(.{$n})(.*)$/s;
With unpack (Not for the faint of heart):
( $chars, $string ) = unpack "a$n a@{[length($string)-$n]}", $string;
| Answer: How do I pull n characters off the front of a string? contributed by xtrem my ($x,$y) = ($string =~ /^(.{n})(.{n})/);
This will put the 1st n char into $x
and the 2nd n chars into $y
without touching $string...
if you want the rest of the chars:
my ($x, $y, $rest) = ($string =~ /^(.{n})(.{n})(.*)/);
if you don't like regexp's you can allways try substr |
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
Log In?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others having an uproarious good time at the Monastery: (4) As of 2021-02-27 18:53 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|