Re: loops learning difficulties (was Re: looping insert) (was re: looping problem)
Seamus To review, our solution to the classic "Native American" problem was: The heading lifting is being done in the tag: setting the starting and ending values for year setting the increment for year testing for the end value of year. We used this style of loop because we have a predetermined number of steps, 377 (2001 - 1624). Even if we change the problem to specify biannual interest, we still have a fixed number of steps... we just change the step value to 2. Then I redefined the problem to: The Dutch purchased Manhattan Island from the "Native Americans" for $18.00 in er, ah... lets say 1624. If the money were invested at 4%, compounded annually, when would the value exceed $200,000? Now, we can't determine in advance how many steps are necessary, because that depends on the calculation within the loop. if we write this out abstractly, we have: Start with an amount of $18,00 Start at a year of 1624 Repeat Multiply the amount by 1.04 Add 1 to the year until the Amount is Greater Than 2000 To translate this into CF we have: In the above, the tag does a lot less work than our prior example... it only tests if the loop should continue (while the condition is still true). As before, the body of the loop computes a new amount... this is used in the condition that controlls the loop In addition, we increment the year within the body of the loop, because it is the answer we seek (and it is no longer being computed in the tag If we want to compute biannual interest, we merely change the year increment within the loop body: So, we have covered the two basic types of loops: a FOR loop (predetermined number of steps) A WHILE loop (while a condition is true - unknown number of steps) CF, in its benevolence, provides us with some specialized loops so we can easily iterate over complex structures: lists, queries and collections. These are, in fact, just a more-convenient format of a FOR loop. Next time. HTH Dick ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
Re: loops learning difficulties (was Re: looping insert) (was re: looping problem)
Seamus Some people take to the concepts of loops naturally... for others it is a mental block. Not to worry! The classic programming problem which illustrates the need for loops is the "Native American" Problem: The Dutch purchased Manhattan Island from the "Native Americans" for $18.00 in er, ah... lets say 1624. If the money were invested at 4%, compounded annually, what would it be worth today. To solve you could write out: Year: 1624 Value: $18.00 Year: 1625 Value: $18.00 * 1.04 Year: 1626 Value: $18.00 * 1.04 * 1.04 . . . so we would do the 375 steps to get to 2001 Now if we look closely, we notice a certain consistency in the operations performed in each step: the year is incremented by 1 the amount is incremented by 4& ( Amount * 1.04 ) Regardless of which step (year) the calculations are always the same... the 2 operations are repeated over and over. To write this out abstractly we could say: Start with an amount of $18,00 Start at a year of 1624 Repeat Add 1 to the year Multiply the amount by 1.04 until the year is equal to 2001 What controls the number of times we repeat, is the year (incremented) Now, we take this and rewrite this in CF" Now if you compare the two, you see that the tag combines the operations of: setting the starting and ending values for year setting the increment for year testing for the end value of year. So all we have to do is compute the amount and indicate the end of the loop. Now, if you change the problem slightly: The Dutch purchased Manhattan Island from the "Native Americans" for $18.00 in er, ah... lets say 1624. If the money were invested at 4%, compounded annually, when would the value exceed $200,000? we'll cover that in the next installment, HTH Dick At 11:17 AM +1000 7/13/01, Seamus Campbell wrote: >Many thanks again. > >Slightly OT - I have learnt CF without any background in programming - I >have a huge problem getting loops and lists to work - I can understand the >general principles but just can't seem to be able to learn to implement >specific code. > >For instance I can see what you are doing in the code below and I can adapt >code that other people have done (often by trial and error rather than logic) >BUT i have huge problems coding loops and lists on my own. > >Is this normal - or am I just slow??? > >Any ways to learn how to "roll my own" ??? > >Many thanks >Seamus > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
loops learning difficulties (was Re: looping insert) (was re: looping problem)
Many thanks again. Slightly OT - I have learnt CF without any background in programming - I have a huge problem getting loops and lists to work - I can understand the general principles but just can't seem to be able to learn to implement specific code. For instance I can see what you are doing in the code below and I can adapt code that other people have done (often by trial and error rather than logic) BUT i have huge problems coding loops and lists on my own. Is this normal - or am I just slow??? Any ways to learn how to "roll my own" ??? Many thanks Seamus At 11:37 pm 12/07/01 , you wrote: >There's a couple of ways this can be handled. But, here's what I usually do. > > > >Delete * from tblClientCategory WHERE clientid=#form.clientid# > > > >(UPDATE tblClientCategory SET CategoryID=#listgetat(form.categoryid, i)# >WHERE >tblClientCategory.ClientID=#form.clientid#) > > > > >Essentially, instead of trying to test for the existence of the categoryid, >you're just deleting the old and then inserting all the new. >is optional here. It ensures that if your second (looped) query doesn't >work, that you haven't lost your original data. However, if you're >potentially looping through lots of fields, with non-critical data, you >might consider the tradeoffs. > >-Deanna > > > > >Deanna Schneider >Interactive Media Developer >UWEX Cooperative Extension Electronic Publishing Group >103 Extension Bldg >432 N. Lake Street >Madison, WI 53706 >(608) 265-7923 > > > > > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
looping insert (was re: looping problem)
There's a couple of ways this can be handled. But, here's what I usually do. Delete * from tblClientCategory WHERE clientid=#form.clientid# (UPDATE tblClientCategory SET CategoryID=#listgetat(form.categoryid, i)# WHERE tblClientCategory.ClientID=#form.clientid#) Essentially, instead of trying to test for the existence of the categoryid, you're just deleting the old and then inserting all the new. is optional here. It ensures that if your second (looped) query doesn't work, that you haven't lost your original data. However, if you're potentially looping through lots of fields, with non-critical data, you might consider the tradeoffs. -Deanna Deanna Schneider Interactive Media Developer UWEX Cooperative Extension Electronic Publishing Group 103 Extension Bldg 432 N. Lake Street Madison, WI 53706 (608) 265-7923 ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists