Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Chopping the beginning of a string?

by Anonymous Monk
on Oct 29, 2003 at 13:16 UTC ( [id://302991]=perlquestion: print w/replies, xml ) Need Help??

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

What are the various way one can remove a character from the beginning of a string, e.g., reverse() + chop(), substr(), etc., and which one(s) would you recommend and why?

Replies are listed 'Best First'.
Re: Chopping the beginning of a string?
by jeffa (Bishop) on Oct 29, 2003 at 13:31 UTC
    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)
    

      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
Re: Chopping the beginning of a string?
by Roger (Parson) on Oct 29, 2003 at 13:30 UTC
    There are many ways to do this in Perl. The following are just a few...
    my $string = 'abcdefghijklmn'; # regular expression $string =~ s/^a//; # chop the first character 'a' $string =~ s/^.//; # chop the first character # substring $string = substr($string, 1); # chop the first charater $string = substr($string, 3); # chop 3 char's off string # etc...
    chop and substr would certainly be the fastest way...

Re: Chopping the beginning of a string?
by hmerrill (Friar) on Oct 29, 2003 at 14:05 UTC
    As Roger and jeffa already pointed out, 'substr' can be used. But completeness I'll mention 'unpack'. The Perl Cookbook p.3 & 4 mentions substr and unpack to access substrings. To remove the 1st character from a string, you could use unpack this way:
    ($first, $rest) = unpack("A1 A*", $original_string);
    I don't know performance wise which is better, but I'm guessing 'substr' would be test if you just need to remove the first character. But if you need to separate and capture more than just one substring from the line, and the substrings are at the same location in every line, then unpack might be test. If performance is your concern, just create one loop that does one 1000(or 10,000, or 100,000) times, and then another loop that does the other 1000 times - capture the time at the start and end of each, and compare.
Re: Chopping the beginning of a string?
by bart (Canon) on Oct 29, 2003 at 15:35 UTC
    My personal recommendations would be s///, or substr on the left hand side of an assignment, or the 4 argument substr. A demo of each:
    ($chopped) = $s =~ s/^(.)//s; # or $s =~ s/^.//s;
    substr($s, 0, 1) = ""; # no way to get the value back, though... # Note: you need the parens. Precedence of the assigment is higher tha +n that of the comma.
    $chopped = substr $s, 0, 1, ""; # or substr $s, 0, 1, "";
    Why? I think these must be quite fast, and simple enough. I doubt reverse is that fast, especially on long strings.

    You can always use substr as a function, but I'd only do that if I need to keep the original value of the scalar, and get a modified copy.

Re: Chopping the beginning of a string?
by delirium (Chaplain) on Oct 29, 2003 at 14:07 UTC
    substr can be used to assign a null value to the location you want erased. e.g.:

    perl -le '$g="test";substr($g,0,1)="";print $g' est
Re: Chopping the beginning of a string?
by l2kashe (Deacon) on Oct 29, 2003 at 16:00 UTC
    my $str = 'abcdef'; $str = $1 if $str =~ m/^.(.*)$/;

    use perl;

Re: Chopping the beginning of a string? (A chip off the old block).
by BrowserUk (Patriarch) on Oct 29, 2003 at 23:05 UTC

    Anyone up for adding chip() to Scalar::Util? I know I've wanted this function on many, many ocassions.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re: Chopping the beginning of a string?
by ambrus (Abbot) on Apr 11, 2006 at 14:46 UTC
Re: Chopping the beginning of a string?
by bradcathey (Prior) on Oct 30, 2003 at 02:53 UTC
    Of course, there are the ways eluded to in your original question, but just for fun (since I'm learning regex's these days):
    use strict; my $string = "This is a test.\n"; $string =~ s/^(.{1})(.*)/$2/;

    Update
    I've got to start reading the existing replies before I make mine! My regex has about 3x more characters than needed, especially after looking at Roger's and some of the others. So many ways, so little time.
Re: Chopping the beginning of a string?
by Anonymous Monk on Oct 30, 2003 at 03:47 UTC
    There may be more than one way to do it, but substr is the only correct way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found