Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Anonymous Monk
on Apr 24, 2001 at 20:03 UTC ( #75118=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (strings)

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.

Originally posted as a Categorized Question.

  • Comment on How do I pull n characters off the front of a string?

Replies are listed 'Best First'.
Re: How do I pull n characters off the front of a string?
by arturo (Vicar) on Apr 24, 2001 at 20:59 UTC

    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);
Re: How do I pull n characters off the front of a string?
by davido (Cardinal) on Oct 03, 2003 at 01:31 UTC
    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;

      unpack:
      ($chars, $string) = unpack "a$n a*", $string;
      The four parameter substr is clearly the cleanest solution though.

      Makeshifts last the longest.

Re: How do I pull n characters off the front of a string?
by perlmonkey (Hermit) on May 02, 2001 at 11:48 UTC
    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)
Re: How do I pull n characters off the front of a string?
by cbraga (Pilgrim) on May 01, 2001 at 22:47 UTC
    Or this way:
    #assuming $n contains the number $string =~ s/^(.{$n})//; $first_n = $1;

    Originally posted as a Categorized Answer.

Re: How do I pull n characters off the front of a string?
by xtrem (Novice) on Apr 24, 2001 at 20:29 UTC
    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
Re: How do I pull n characters off the front of a string?
by Masem (Monsignor) on Apr 24, 2001 at 20:26 UTC
    my ($x,$y) = ( $string =~ /^(.{$n})(.*)$/ );

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://75118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-09-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?