ColdFusion in Context: Building Cryptograms
Suppose you have some empty space in your newsletter and a pithy quote for your readers to remember. One way to get extra mileage out of your quote is to express it as a cryptogram. As we know, cryptograms are merely simple substitution cyphers where you substitute one letter for another. Here's a way to build one.
Rearrange the Alphabet
You want to rearrange all letters so no letter keeps its original position.
Put all code in crypto1.cfm. Create the alphabet with spaces between letters. There is probably a simpler starting point, but this coding is visual and easy. Use the listToArray function with delimiter of space to convert the list to an array.
Walk through the array from the end to the beginning, swapping the current position with any random position below it. (Yes, position 2 is always swapped with whatever's left in position 1, but the contents of position 1 have usually changed several times before that point, so this works.) Then convert the array back into a list. A space is used again for a delimiter here, but any delimiter consistent with the next step will work.
<!--- Rearrange alphabet so no letter keeps its position --->
<cfset StartList="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z">
<cfset StartArray=listToArray(StartList," ")>
<cfloop from=26 to=2 step=-1 index="Pos">
<cfset NewPos=randrange(1, Pos-1)>
<cfset Dummy=arraySwap(StartArray,Pos,NewPos)>
</cfloop>
<cfset EndList=arrayToList(StartArray," ")>
Remove Spaces; Show the True and Fake Alphabet
It's easy to work with these alphabets if they don't have any delimiters; so, remove them. Use the pre tag to force a monospaced font for this "key" to the puzzle. It's actually a bit awkward to get the alphabets to line up one above the other. If you use an html break, you'll get two lines. If you simply start the next variable on the next line, it will disobey the preformatting tag and show up on the same line. However, chr(10), a linefeed, will do what you want.
<!--- Remove spaces; show true and fake alphabet --->
<cfset ShowList=replace(StartList," ","","all")>
<cfset ShortList=replace(EndList," ","","all")>
<cfoutput><pre>
#ShowList##chr(10)#
#ShortList#</pre></cfoutput>
Build the Output if an Input is Present
After you set defaults and discover that an input is present, it takes a bit of thought to create an output using the fake alphabet simply and quickly. A naive method would convert one position of the input message at a time. Thus, a 200-position message would take 200 passes through 26 letters. (Ouch!) Forcing the input to capital letters and converting every A, every B, every C and so forth to its equivalent sounds good, but because you're working with the string you're changing, you'll wind up changing many characters over again to something else; this is definitely not what you want.
The key to the problem is to convert the input to upper case (for looks) but work on a copy of the input that you've forced to lower case. Convert the lower-case As to their equivalent (which will be an upper-case letter). Repeat for the lower-case Bs, and in 26 passes, you're done. Because you look for only lower-case letters, and you convert them to upper-case letters, you won't accidentally convert something you've already converted, and the puzzle takes the traditional form.
<!--- Build output if input is present --->
<cfparam name="InText" default="">
<cfparam name="OutText" default="">
<cfif len(trim(InText))>
<cfset InText=uCase(InText)>
<cfset OutText=lcase(InText)>
<cfloop from=1 to=26 index="LetterNr">
<cfset OutText=replace(OutText,lcase(chr(64+LetterNr)),mid(ShortList,LetterNr,1),"all")>
</cfloop>
</cfif>
Accept Input; Show Output
You could have just an input field and print the output to the screen, but using a field for output as well lets you see the input and output in a parallel format with similar word wraps. Use the textarea tag. Specify that word wrap should be virtual to keep Netscape happy.
<!--- Accept input; show output --->
<form action="crypto1.cfm" method="post">
<textarea name="InText" cols="60" rows="3" wrap="virtual"><cfoutput>#InText#</cfoutput></textarea>
<p>
<textarea name="OutText" cols="60" rows="3" wrap="virtual"><cfoutput>#OutText#</cfoutput></textarea>
<input type="submit" name="doit" value="Go">
</form>
Done
Browse crypto1.cfm, enter a phrase, and watch the fun. You wouldn't try to protect anything with this; but it can bring hours of enjoyment to your readers. (People buy whole books of this stuff.) =Marty=