Find a letter on a position in the Alphabet

Just needed a quick way to find a letter in the alphabet on a postion.

So for example, postion 1 returns 'A' and position 26 returns 'Z'.

Step 1: Function

<cffunction name="getAlphabetLetter" access="public" returntype="string">
    <cfargument name="position" type="numeric" required="true">
    
    <!--- Thanks to Ryan and Shane..... --->
    <cfif arguments.position gte 1 and arguments.position lte 26>
     <cfreturn chr(64+arguments.position) />
    <cfelse>
        <cfreturn "not in alphabet">
    </cfif>
    
</cffunction>

 Step 2: Call function

<cfoutput>#getAlphabetLetter(26)#</cfoutput>

returns: Z

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Ryan's Gravatar or...
<cfif arguments.position gte 1 and arguments.position lte 26>
   <cfreturn chr(64+arguments.position) />
</cfif>
# Posted By Ryan | 8/22/08 5:10 PM
Shane Zehnder's Gravatar There is no need to create the array.. You could just do:

<cfreturn chr(64+arguments.position) />
# Posted By Shane Zehnder | 8/22/08 5:10 PM
Shane Zehnder's Gravatar lol Ryan beat me to it ;)
# Posted By Shane Zehnder | 8/22/08 5:11 PM
Chris's Gravatar Hi Ernst,

generating an array every time you need this function does not seem necessary... you could store the array in a persistant scope.

But, actually my question is, why don't you simply do
#chr(64+x)#
where x is the position in the alphabet?

Chris
# Posted By Chris | 8/22/08 5:14 PM
Ernst van der Linden's Gravatar Thanks you're both right.....
# Posted By Ernst van der Linden | 8/22/08 5:28 PM
Rene Luijk's Gravatar @ Chris
#chr(64=x) seems to be perfect at times. At other times I think it's nice to have the implementation hidden in a method that is explaining by name what it is doing.

@ Ernst
I would like to have a more formal exception instead of the string ''not in alphabet".
That makes reusing the method more tempting to me.
# Posted By Rene Luijk | 9/7/08 6:28 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.004.