Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

howto strip the first char of a string?

by valentin (Abbot)
on Sep 08, 2004 at 13:05 UTC ( #389333=perlquestion: print w/ replies, xml ) Need Help??
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/^.//;

Comment on howto strip the first char of a string?
Select or Download Code
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?

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

        /J\

        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

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 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 herveus (Parson) 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 (Vicar) 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 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 ambrus (Abbot) on Apr 11, 2006 at 14:27 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://389333]
Approved by pelagic
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-09-22 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (184 votes), past polls