http://www.perlmonks.org?node_id=389333

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

Hi, I search for the recommended way to strip the first character of a string. My ideas are
substr $_, 1;
and
s/^.//;

Replies are listed 'Best First'.
Re: howto strip the first char of a string?
by gellyfish (Monsignor) on Sep 08, 2004 at 13:23 UTC

    Those are both perfectly reasonable ways of removing the first character of a string. Other far less reasonable ways might include:

    my @foo = split //; shift @foo; $_ = join '', @foo; or my $foo = reverse($_); chop($foo); $_ = reverse($foo); or $_ = unpack "xA*", $_;
    and so forth.

    /J\

      Excellent! Could anti-golf be the new golf?
        Notwithstanding gellyfish's excellent diversion, there is no "longest" way to do it. You can always add another semi-colon.

        On the other hand, once you whittle a solution down to the empty string, I'm reasonably sure* that nothing shorter will show up.

        And for the longest contest, after the first few TB solutions, we'll all grow tired of waiting for the latest entry to download.

        * Solutions by TheDamian excepted.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

        It became almost a tradition in clpm a few years back.

        /J\

Re: howto strip the first char of a string?
by thospel (Hermit) on Sep 08, 2004 at 13:26 UTC
    In perl there are rarely recommended ways. Do what makes sense for whatever your exact situation is. The main issue will probably be if you want to change an existing string inplace or make a new string. And your ideas are fine for that.

    New string:
    $new = substr $old, 1

    Modify old string:
    substr($str, 0, 1) = ""
    or
    substr $str, 0, 1, ""
    or
     $str =~ s/^.//s
    (notice the s modifier, which is needed if you want the code to also work if the first character is a \n. You may want to replace the ^ with \A for extra paranoia)

    The other main point to consider is what you want to happen if the original string is empty. Error ? Warning ? Silence and just give the empty string as result ? All solutions are easily modified for whatever you want there.

Re: howto strip the first char of a string?
by Anonymous Monk on Sep 08, 2004 at 13:25 UTC
    substr $_, 0, 1 = "";
    or substr $_, 0, 1, "";

      The first of these does not work for me (at least on perl 5.005_03) The second is cunning enough though.

      perl -lpe 'substr $_, 0, 1 = "";' Can't modify constant item in scalar assignment at -e line 1, near """ +;" Execution of -e aborted due to compilation errors. perl -lpe 'substr $_, 0, 1, "";' this is his is
      The first one will work with a minor tweak
      perl -lpe 'substr($_, 0, 1) = "" ' test est

      Cheers,
      R.
Re: howto strip the first char of a string?
by Aristotle (Chancellor) on Sep 08, 2004 at 22:03 UTC
    s/^.//;

    What if the first character is a newline? :-) Don't forget the /s! As an aside, FWIW, the s/// is much less efficient than the substr solution here.

    Another overengineered way:

    use Fcntl qw( :seek ); $_ = "12345"; { open my $fh, "<", \$_; seek $fh, 1, SEEK_SET; local $/; $_ = <$fh>; }

    Makeshifts last the longest.

Re: howto strip the first char of a string?
by herveus (Prior) on Sep 08, 2004 at 13:34 UTC
    Howdy!

    Well, there's always chop reverse; reverse;

    yours,
    Michael

      Did you try running that? Perhaps you meant the pseudo-code reverse; chop; reverse; which spelled out in Perl would be $_ = reverse; chop; $_ = reverse;?

      If not, I don't understand how you'd expect chop reverse; reverse; to do anything useful.

      ihb

      Read argumentation in its context!

        Well, in the insane-o universe where reverse was an lvalue, chop reverse is all you'd need. That, however is certainly not the case. It's funny just to think, though, about someone implementing an lvalue reverse function... can you lvalue a tied scalar? I should look into that :-p
        ------------ :Wq Not an editor command: Wq
Re: howto strip the first char of a string?
by Anonymous Monk on Sep 08, 2004 at 14:29 UTC
    chop substr $_, 0, 1
Re: howto strip the first char of a string?
by QM (Parson) on Sep 08, 2004 at 14:33 UTC
    I find a pencil sharpener to work in many cases. But if you crank too many times, you'll end up just holding the eraser.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: howto strip the first char of a string?
by valentin (Abbot) on Sep 08, 2004 at 15:35 UTC
    Thank you all, for that much insperation!
Re: howto strip the first char of a string?
by ambrus (Abbot) on Apr 11, 2006 at 14:27 UTC