Beginner wrote:
>
> I am being a bit lazy here. I already had this hash defined (cut &
> pasted from another file) and didn't want to re-type it.
>
> my %char = (
>   65 => 'a',
>   66 => 'b',
>   67 => 'c',
>   68 => 'd',
>   69 => 'e',
>   70 => 'f',
>   71 => 'g',
>   72 => 'h',
>   73 => 'i',
>   74 => 'j',
>   75 => 'k',
>   76 => 'l',
>   77 => 'm',
>   78 => 'n',
>   79 => 'o',
>   80 => 'p',
>   81 => 'q',
>   82 => 'r',
>   83 => 's',
>   84 => 't',
>   85 => 'u',
>   86 => 'v',
>   87 => 'w',
>   88 => 'x',
>   89 => 'y',
>   90 => 'z',
>   );
>
>
> But now I need to find the key given the value. EG: What is the ASCII
> value for 'a'?
>
> Am I being daft, or is the only way to push all the valuse into and
> array and loop through it?

Hello

The values can be calculated instead of being held in a hash. Your hash
relates the character code for the upper case letters to their lower
case equivalents. This subroutine will do the same thing for you:

  sub char {
    lc chr shift;
  }

so that you can use char(65) instead of $char{65}.

To do the reverse operation, use

  sub code {
    ord uc shift;
  }

which will let you call code('a') giving 65.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to