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

Hi there, I wanted to know if there are quicker/better ways of translating numbers to letters. At the minute I am doing tr/etc/etc/g. But it doesn't feel nor look right when translating many many letters/numbers.

Replies are listed 'Best First'.
Re: Translating letters
by BrowserUk (Patriarch) on Apr 15, 2012 at 17:37 UTC
    I wanted to know if there are quicker/better ways of translating numbers to letters. At the minute I am doing tr/etc/etc/g.

    No, tr/// is hands down speed winner for char-by-bar translations where the ampping is known at compile time.

    it doesn't feel nor look right when translating many many letters/numbers.

    In most cases, it is possible to use the start-end range facility to produce compact and clear code.

    If that is not the case for your application, post an example. You may be better off evaling a custom tr sub into existence.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Well at the moment, I am doing:
      foreach (@words) { tr/abcdef/123456/; print @words; }
      And the contents of @words are read in from a file. But eventually it will look messy (I think) so I was just wondering of a good/better way to do it (if there are any)
        But eventually it will look messy (I think)

        That doesn't look "messy" to me? Maybe you could post an example of what you think it might "eventually" look like?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Translating letters
by RichardK (Parson) on Apr 15, 2012 at 18:12 UTC

    You could always use ranges if you think it looks better.

    tr/A-J/0-9/
Re: Translating letters
by pvaldes (Chaplain) on Apr 16, 2012 at 12:34 UTC

    Being in mind that there are more than ten letters you can't use really tr for this idea because tr only changes a char by another

    What I want to say is that you can't use tr to change i.e "f" by "23"

    Although nothing is wrong with tr, if your problem is more complex that the example that you provide you probably should think in a hash for this

      mmmh... you ask about traslating numbers to letters but I realize just now that every people are doing exactly the opposite, showing how to traslate letters to numbers... XD.

      tr is the right tool for this, then