320 likes | 569 Views
Loops And Lists. Seree Chinodom Department of Computer Science. Introduction. Loops - many types List handling. Loop Types. Here is what we will be covering: Index Loops Conditional Loops Looping over a Query Looping over a List Looping over a Structure System
E N D
Loops And Lists Seree Chinodom Department of Computer Science
Introduction • Loops - many types • List handling
Loop Types Here is what we will be covering: • Index Loops • Conditional Loops • Looping over a Query • Looping over a List • Looping over a Structure System • Looping over a COM Collection
1.1 Index Loops • “FOR NEXT” loop <CFLOOP INDEX="parameter_name" FROM="beginning_value" TO="ending_value" STEP="increment"> • Nesting
1.2 Conditional Loops • “DO WHILE” loop <CFSET StopIt = 0> <CFLOOP CONDITION="StopIt LESS THAN OR EQUAL TO 5"> <CFSET StopIt = RandRange(1,10)> <HR> </CFLOOP> • Can exit a loop with <CFBREAK>
1.3 Query Loops • CFQUERY plus <CFLOOP QUERY="GetEmail"> </CFLOOP> • Nesting • more tags inside • Slower
1.4 List Loops • “FOR EACH” loop <CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> <CFOUTPUT>#ListElement#</CFOUTPUT><BR> </CFLOOP> • Other delimiters
SQL IN clause Form.state = “’MD’, ‘DC’, ‘VA’” SELECT * FROM Customer WHERE State_ID IN (#Form.state#)
List handling • ListAppend, ListPrepend • ListInsertAt • ListSetAt, ListGetAt • ListFirst, ListLast, ListRest • ListDeleteAt • ListFind, ListFindNoCase • ListContains, ListContainsNoCase • ListChangeDelims
List handling Part 2 • ListToArray, ArrayToList • ReplaceList, ListLen • QuotedValueList, ValueList • GetClientVariablesList
1.5 Structure Loops • “FOR EACH” Loops 2 <CFLOOP COLLECTION=#Departments# ITEM="person"> #person#, #StructFind(Departments, person# </CFLOOP>
1.6 COM Loops • FOR EACH OBJECT Loops <CFLOOP COLLECTION=#FFUNC# ITEM=file2> <CFOUTPUT> #file2.name# <BR> </CFOUTPUT> </CFLOOP>
ListLen • Returns the number of elements in the list. ListLen(list [, delimiters ]) • ex <CFOUTPUT> #ListLen(“1,2|3,4”, “|”)#<BR> #ListLen(“1,2|3,4”)#<BR> #ListLen(“1,2|3,4”, “,|”)#<BR> </CFOUTPUT>
ListFirst • Returns the first element of the list. • Syntax ListFirst(list [, delimiters ]) • list: List whose first element is being retrieved. • Delimiters: Set of delimiters used in list.
ListLast • Returns the last element of the list. • Syntax ListLast(list [, delimiters ]) • list : List whose last element is being retrieved. • delimiters :Set of delimiters used in list.
ListRest • Returns list without its first element. Returns an empty list (empty string) if list has only one element. • Syntax ListRest(list [, delimiters ]) • list: List whose elements are being retrieved. • delimiters: Set of delimiters used in list.
ListAppend • Returns list with value appended behind its last element. • Syntax ListAppend(list, value [, delimiters ]) • list : Any list. • value: Number or list being appended. • delimiters :Set of delimiters used in list.
ListPrepend • Returns list with value inserted at the first position, shifting all other elements one to the right. • Syntax ListPrepend(list, value [, delimiters ]) • list : Any list. • Value: Number or list being appended. • Delimiters :Set of delimiters used in list.
ListChangeDelims • Returns list with all delimiter characters changed to new_delimiter string. • Syntax ListChangeDelims(list, new_delimiter [, delimiters ]) • list : Any list. • Value: Number or list being appended. • Delimiters :Set of delimiters used in list.
ListGetAt • Returns the element at a given position. • Syntax ListGetAt(list, position [, delimiters ]) • list : List whose element is being retrieved. • Position: Positive integer indicating the position of the element being retrieved. • Delimiters :Set of delimiters used in list.
ListInsertAt • Returns list with value inserted at the specified position. • Syntax ListInsertAt(list, position, value [, delimiters ]) • list : Any list • Position: Position where the value is being inserted. The first position in a list is denoted by the number 1, not 0. • Value :Number or list being inserted. • Delimiters :Set of delimiters used in list.
ListSetAt • Returns list with value assigned to its element at specified position. • Syntax ListSetAt(list, position, value [, delimiters ]) • list : Any list. • Position: Any position. The first position in a list is denoted by the number 1, not 0. • Value : Any value. • Delimiters : Set of delimiters.
ListDeleteAt • Returns list with element deleted at the specified position. • Syntax ListDeleteAt(list, position [, delimiters ]) • list : Any list. • Position: Positive integer indicating the position of the element being deleted. The starting position in a list is denoted by the number 1, not 0. • Delimiters : Set of delimiters used in list.
ListFind • Returns the index of the first occurrence of a value within a list. Returns 0 if no value is found. The search is case-sensitive. • Syntax ListFind(list, value [, delimiters ]) • list: List being searched. • Value : Number or string that is to be found in the items of the list. • Delimiters: Set of delimiters used in the list.
ListFindNoCase • Returns the index of the first occurrence of a value within a list. Returns 0 if no value was found. The search is case-insensitive. • Syntax ListFindNoCase(list, value [, delimiters ]) • list : List being searched. • Value : Number or string being sought among elements of list. • Delimiters : Set of delimiters used in list.
ListContains • Returns the index of the first item that contains the specified substring. The search is case-sensitive. If the substring is not found in any of the list items, it returns zero (0). • Syntax ListContains(list, substring [, delimiters ]) • list: List being searched. • Substring : String being sought in elements of list. • Delimiters: Set of delimiters used in list.
ListContains • Returns the index of the first element of a list that contains the specified substring within elements. The search is case-insensitive. If no element is found, returns 0. • Syntax ListContainsNoCase(list, substring [, delimiters ]) • list: List being searched. • Substring : String being sought in elements of list. • Delimiters: Set of delimiters used in list.
Resource • CFDOCS • Ben Forta Books • cf-talk email list • http://www.houseoffusion.com