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

I have a shell script that calls a Perl one-liner to increment a letter.
NEXTBUILDLETTER=$(perl -e "\$x=ord($BUILDLETTER); \$x++; printf(\"%c\n +\", \$x);")
The problem is... you guessed it... it chokes when it gets to N because the \n gets interpreted as a newline.

I've played with it forever now, and I can't seem to find a way around this. Thoughts?

Replies are listed 'Best First'.
Re: Increment Letter on a Command Line
by Fletch (Bishop) on Feb 06, 2008 at 21:09 UTC

    No need to convert to an integer and what not; ++ does the right thing. You also can cure some of your backwhackitis by just passing the current letter as an argument rather than trying to get it to interpolate.

    NEXTBUILDLETTER=$( perl -le '$l=shift;print++$l' $BUILDLETTER )

    Update: Also keep in mind that that's going to behave slightly saner than using ord, for some value of sane where "AA" follows "Z" rather than "[".

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      This was exactly what I was looking for. Thanks!
Re: Increment Letter on a Command Line
by moritz (Cardinal) on Feb 06, 2008 at 21:10 UTC
    You can avoid the many escapes by using single quotes and reading the letter from STDIN:
    $ echo 'a' | perl -pe 'chomp; $_++; $_.=$/' b

    This uses the magic increment, not by ascii values - but you get the picture.

      Don't forget "-l" (for chomping on input and adding $/ on output). It really helps keep your one-liners slim and trim:
      $ echo 'a' | perl -lpe '$_++;' b