Phoenix.pm: Incrementing Characters in Perl

Jason Riedel jasonriedel at jasonriedel.com
Tue Sep 16 16:49:53 CDT 2003


Craig,

    It would seem that if you are simply doing a single character that is
probably the fastest way. Since your actually modifying the value an sprintf
solution doesn't work, this would only allow you to print the value as
letter or number. The only other thing I know of is with unpack C*, but this
would only benefit you if you were doing a multi character string. So like I
said, I think you have the best way. For more information about anything I
mention look in the perl cookbook page 10 or just email me.

Jason Riedel

----- Original Message ----- 
From: "Frooninckx Craig - cfroon" <Craig.Frooninckx at acxiom.com>
To: <phoenix-pm-list at happyfunball.pm.org>
Sent: Tuesday, September 16, 2003 2:14 PM
Subject: Phoenix.pm: Incrementing Characters in Perl


> I've got a function I'm converting from C to Perl.  One of the things I do
> is add to letters, so for instance my C code is
> char letter;
>
> letter = 'A';
> letter += 3;
>
> which would make letter 'D'.
>
> In Perl this doesn't work
> $letter = 'A';
> print $letter , "\n";
> $letter = $letter + 4;
> print $letter , "\n";
>
> prints 'A' , '4'
>
> Using chr and Ord I found a way to make it work.
>
> $letter = 'A';
> print $letter , "\n";
> $letter = chr(ord($letter) + 4);
> print $letter , "\n";
>
> But I was wondering if you could think of anything slicker, using regex,
tr,
> or something like that.  No big deal as I do have a solution.  Just
> wondering if there is a better way to do it.
>
> -Craig
>
>
>
>
>
> **********************************************************************
> The information contained in this communication is
> confidential, is intended only for the use of the recipient
> named above, and may be legally privileged.
> If the reader of this message is not the intended
> recipient, you are hereby notified that any dissemination,
> distribution, or copying of this communication is strictly
> prohibited.
> If you have received this communication in error,
> please re-send this communication to the sender and
> delete the original message or any copy of it from your
> computer system. Thank You.
>




More information about the Phoenix-pm mailing list