in reply to Chop command that removes the first character

The naive approach of

perl -le "$f='hello world'; chop reverse $f; print $f"

doesn't work sadly, and the correct approach of

perl -le "$f='hello world'; $f=reverse $f;chop $f;$f=reverse $f; print + $f"

is far too clumsy, so relying on the fact that substr returns an lvalue, I'd use:

perl -le "$f='hello world'; substr($f,0,1)=''; print $f"

Of course, there are many many more methods of removing the first character of a string:

perl -le "$f='hello world'; $f=~s!^.!!; print $f"

or

perl -le "$f='hello world'; $f=~s!.!!; print $f"

which I guess is the shortest variant, clocking in at 9 characters.

Replies are listed 'Best First'.
Re^2: Chop command that removes the first character
by ikegami (Patriarch) on Jan 15, 2007 at 23:44 UTC
    perl -le "$f='hello world'; $f=~s!^.!!; print $f" perl -le "$f='hello world'; $f=~s!.!!; print $f"

    should be

    perl -le "$f='hello world'; $f=~s!^.!!s; print $f" perl -le "$f='hello world'; $f=~s!.!!;s print $f"

    The first character could be a newline.