Phoenix.pm: Incrementing Characters in Perl

David A. Sinck phx-pm-list at grueslayer.com
Tue Sep 16 17:09:58 CDT 2003


You can increment a string:

$a = 'A';

$a++ for (1..3);  # will when perl golf over the other soln :-)

print "$a\n";

but apparently only one inc at a time.

David


\_ SMTP quoth Frooninckx Craig - cfroon on 9/16/2003 14:14 as having spake thusly:
\_
\_ 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