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

Hello Monks,
How does one remove the first character of a string in perl? I guess this would be the oposite of chop().
Thanks

Replies are listed 'Best First'.
Re: removing first char of string
by jacques (Priest) on Feb 10, 2003 at 01:09 UTC
    $string =~ s/^.//s;
    This will also work: substr ($string, 0, 1) = '';
Re: removing first char of string
by data64 (Chaplain) on Feb 10, 2003 at 00:55 UTC
Re: removing first char of string
by ihb (Deacon) on Feb 10, 2003 at 01:08 UTC
    One popular way is to use a substitution:   s/.//s; ihb
      cool. Thanks so much
Re: removing first char of string
by jmcnamara (Monsignor) on Feb 10, 2003 at 09:00 UTC

    Use substr in the 4 arg form as shown above or in the 2 arg form:
    $str = substr $str, 1; # Or in case the string is empty $str = substr $str, 1 if length $str;

    --
    John.

Re: removing first char of string
by Cabrion (Friar) on Feb 10, 2003 at 02:45 UTC
    Or maybe . .
    $string = reverse(chop(reverse($string)));
    Doh! Thanks ihb.
    chop(reverse$string)); $string = reverse($string);
    Note to self: Don't offer advice while drinking beer...

    Sorry all.

      You didn't actually try that, did you? :)

      It won't work, and that's because chop() tries to modify its argument, i.e. it wants an lvalue. The return value of chop() returns the (last) removed value, so even if chop() could chop the reverse()-expression $string would hold the first char of itself, and the outer reverse would be useless.

      ihb