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

Greatings to all you monks full of wisdom, i'm just a little novice and would like to know if there is a better way to translate a charater into the corresponding number (a/A => 1, b/B => 2, ... ) and the other way around than using a hashtable... perhaps by using the tr// function - i dont know Thanx for your wisdom NaSe

Replies are listed 'Best First'.
Re: translate a character into a number
by jmcnamara (Monsignor) on Apr 25, 2002 at 09:33 UTC

    How about something like this:
    my $char = 'A'; my $num = -64 + ord uc $char; $char = chr(64 + $num); # Translate back (to uppercase)

    --
    John.

      my $char = 'A'; my $num = -64 + ord uc $char;
      Won't work for EBCDIC, but I'm guessing that's not a widespread problem :)
      --
      Mike
      thats infact a good idea ... merci