Mar 25, 2008

Coldfusion Powerball lottery pick tool

Ever wanted to pick lottery numbers with your own tool? This tool is setup for the powerball. and has set by step explanations.

<!---in coldfusion pick random values is a pain, because if you need five numbers changes are you will get duplicates.--->

<!---We have added some code to deal with that.--->

<!---First we are setting a parameter to create an array called randArray.--->

<cfparam

name="randArray"

type="any"

default="#ArrayNew(1)#" />

<!---I am setting a loop incremental to zero because I need to work with it in my loop. I need five numbers, but first I set it to zero...stay with me.--->

<cfset loopIncremental = 0>

<!---Now we are creating a conditional loop to check to see if the loop is less than five. It is so we are are going to loop and add 1 to the loop incremental until it hits five then stop (see we need five white balls). --->

<cfloop condition="loopIncremental lt 5">

<!---The arrRandRange is the range of values to pick numbers from. In powerball we need 1 through 54.--->

<cfset arrRandRange = randrange(1, 54)>

<!---We are now taking our array we set above int he parameter and change it to a list--->

<cfset randArrayToList = arraytolist(randArray)>

<!---Now we are checking to see if the list has a duplicate value. and setting that to an if statement which goes like this. If the array/list has the number we just randomly picked, we do nothing. If we have a fresh number we append the array and add that value to it.--->

<cfif listfind(randArrayToList, #arrRandRange#)>

<cfelse>

<!---here is where we add the value if the 'condition' is non-duplicate.--->

<cfset ArrayAppend(randArray,"#arrRandRange#")>

<!---we then increment the loop incremental by 1 (see if if it doesn't get added to the array we are still one number short and so we do not set the incriment. Clear as mud?--->

<cfset loopIncremental = #loopIncremental# + 1>

<!---then we exit the if statement--->

</cfif>

<!---next we exit the loop--->

</cfloop>

<cfoutput>

<!---I'm probably doing a usless step here but my basic effort is to sort the array's numbers from lowest to highest and then output the numbers to the screen.--->

<cfset xArrayToList = ArrayToList(randArray)>

<cfset yListSort = listsort(xArrayToList, "numeric")>

#yListSort#

</cfoutput>

<!---And for my last trick I am grabbing one red ball from a range 1 through 50--->

<cfset arrRandRange2 = randrange(1, 50)>

<!---then outputting that value--->

<cfoutput>

#arrRandRange2#

</cfoutput>

and abracadabra I have my lottery number picks...I hope they are winners.

Labels: , , ,

Mar 24, 2008

Looping backwards over a list or array

Many months back I was on a job interview and someone asked me to loop backwards over a name, essentially spelling it backwards. What an odd request. I fumbled around and was totally stumped. I didn't get the job, but you can bet your ass I figured out how to loop backwards over a list/array or whatever. Below is the proof of concept example code.

Enjoy.

<!---Loop backwards example.--->

<!---First we are going to create a list.--->

<cfset myList = "F,r,a,n,k,T,u,d,o,r">

<!---Next we are going to create an array out of it.--->

<cfset myArray = listToArray(myList)>

<!---Now weare going to set up our loop with parameters.--->

<br>

<!---Note that I am setting the from and to max to min and

setting step to -1 so it can step backwards throught he loop.

--->

<cfoutput>

<cfloop from="#arrayLen(myArray)#" to="1" step="-1" index="i" >

#myArray[i]#<br>

</cfloop>

</cfoutput>

Labels: , , , ,

Coldfusion try/catch example.

Like other languages, Coldfusion has the ability to attempt code and then provide a custom error if it fails. This is good if you cannot test your code and example would be if you are waiting for other groups to get database development done, or server environment constraints.

So here is a simple example.

Say I have a query that adds favorites to my table first I am going to set some simple variables

<cfset me = "Frank">

<cfset myFav = "Franklin">

Next, I am going to set up my try/catch code Note I have a primary key made up of fvrt_src, fvrt_sel, meaning I cannot add it twice and if I refresh the screen the error will be produced.

<cftry>

<cfquery

datasource="#request.dsn#"

name="addingToFav">

insert into favorites (fvrt_src, fvrt_sel)

values ('#me#', '#myfav#')

</cfquery>

If the error is produced the catch will sense the database error and produce a nice controlled session error instead of a screen with gibberish (client confusion screens).

<cfcatch type="database">

<cfset session.favAddError = "User has already been added to your favorties<br>

please check your favorites list above.">

</cfcatch>

</cftry>

Next if the session has been created I display the error message below.

<cfif isdefined(“session.favAddError”)>

<cfoutput>

#session.favAddError#

</cfoutput>