Test Your Knowledge
As part of the relaunch of their site, the folks at depressedpress.com
(http://www.depressedpress.com)
are presenting a series of CFML challenges. The first is converting a Query to an Array using
only CFML as fast as you can.
Full details (including the site's own proposed solution) can be found at:
http://www.depressedpress.com/DepressedPress/Content/ColdFusion/Challenge/Index.cfm.
The site will be taking solutions and posting the best ones. There is no set deadline on this contest, so think long and hard, folks!
Last Weeks Questions
- Name all the CFLOOP types and what they do.
- Index loop - Takes 2 numbers and loops from the first (FROM) to the second (TO). The steps and direction of the count between these two numbers can also be controled.
- Conditional Loops - Takes a conditional statment and as loon as it is true will run a loop. This look has no Index or counter so you must write in code to stop the loop. When an infinate loop occurs, this loop type is usually the culprit.
- Looping over a Query - Loops over a query in the same way a CFOUTPUT does, looping over every row in the query record set. During each iteration of the loop, the columns of the current row will be available for output. CFLOOP allows you to loop over tags that can not be used inside CFOUTPUT. A CFOUTPUT must still be used for the evaluation of variables.
- Looping over a List - Takes a list (with a default or defined delimiter) and loops over each item in the list. The Index attribute is loaded with the current list item on each loop iteration.
- Looping over a COM Collection or Structure - Loops over each item in a structure (ColdFusion) or a collection (returned from COM).
-
- Name all the Loop types in CFSCRIPT and what they do.
- for loop - Takes a number and loops until the conditional part of the loop is fales. The number can be added to in order to make it like an index CFLOOP. It cal also run as an infinate loop where there is no stop condition. This loop has many ways of being used.
- while loop - Loops while a condition is true. Same as conditional CFLOOP
- do-while loop - Same as a while loop, but the test to see if the loop stops is done at the end of the loop rather than the beginning.
- for-in loop - Loops over a structure.
-
- Can you loop over a query in CFSCRIPT and if so, how do you do it?
- Yes
- Assume a query named "queryone". We will loop over each row of the query and then loop over each column in the row.
<CFSCRIPT>
// write the column headers in bold
for(loop0=1; loop0 LTE Listlen(queryone.columnlist); loop0=loop0+1)
WriteOutput(' <b>'&listgetat(queryone.columnlist, loop0)&'</b> ');
//Add a new line after the headers
WriteOutPut('<br>');
//Loop over each record in the query
for(Loop1=1; Loop1 LTE queryone.recordcount; Loop1 = Loop1 + 1)
{
//Write out each column of the record
for(loop2=1; loop2 LTE Listlen(queryone.columnlist); loop2=loop2+1)
WriteOutput(Evaluate('queryone.'&listgetat(queryone.columnlist,loop2)&'[loop1]'));
// Add a new line after the record
WriteOutPut(' <br>');
}
</cfscript>
|