CF Loop issues - need elegent solution

Discussion in 'Programming' started by jessegifford, Oct 24, 2008.

  1. #1
    I have a very non elegent solution that I think can be put in a loop or something like unto it; however, I'm still new to CF and am not sure what to use to do this.

    In my code I've attached I'm using several cfelseif statments and currently this limits the possibilities to the number of these I have (5). I'd like to come up with a solution that allows a variable to be put in place so that it is not limited to just 5. Any Ideas?

    Here is my non-elegent solution:

    
    <cfif (RequestVal("marketCount") EQ 1)>
    			<cfset rsMarketSearched__marketID = splitField[1]>
            <cfelseif (RequestVal("marketCount") EQ 2)>
            	<cfset rsMarketSearched__marketID = splitField[1] & " " & splitField[2]> 
            <cfelseif (RequestVal("marketCount") EQ 3)>
            	<cfset rsMarketSearched__marketID = splitField[1] & " " & splitField[2] & " " & splitField[3]>
            <cfelseif (RequestVal("marketCount") EQ 4)>
    			<cfset rsMarketSearched__marketID = splitField[1] & " " & splitField[2] & " " & splitField[3] & " " & splitField[4]> 
            <cfelseif (RequestVal("marketCount") EQ 5)>
            	<cfset rsMarketSearched__marketID = splitField[1] & " " & splitField[2] & " " & splitField[3] & " " & splitField[4] & " " & splitField[5]>        
            </cfif>  
    
    Code (markup):
     
    jessegifford, Oct 24, 2008 IP
  2. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <cfset _count = requestVal("marketCount")>

    <cfloop from="1" to="#_count#" index="i">
    <cfset rsMarketSearched__marketID += splitField & " ">
    </cfloop>

    <!--- Remove the trailing space --->
    <cfset rsMarketSearched__marketID = trim(rsMarketSearched__marketID)>
     
    robhustle, Oct 24, 2008 IP
  3. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3


    Since the OP mentioned they are new to CF, a few comments. The concept above is on target. But the code assumes the rsMarketSearched__marketID variable already exists before the loop. An error will occur if it does not.

    Also, the "+=" operator probably won't work with strings and may throw an error if the splitField[x] value is not numeric.

    A slight variation is to treat the variable as a list. Then use a space (ie chr(32) as the delimiter to append each value.

    
    <cfset yourList = "">
    <cfloop from="1" to="#maxField#" index="i">
    	<cfset yourList = listAppend(yourList, splitField[i], chr(32))>
    </cfloop>
    
    Code (markup):
     
    cfStarlight, Oct 24, 2008 IP
  4. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you want to get picky, why stop there?

    Look at that splitField array. If you are worried about the existence of rsMarketSearched__marketID before the loop, shouldn't you worry whether or not splitField exists before the loop, too?

    Shouldn't you initialize it or test for existence before you reference it?

    If it does exist, shouldn't you test for type with isArray() before using it?

    And, if it is an array, shouldn't you use arrayLen() to compare with the value of requestVal("marketCount") to make sure that you don't get an ArrayIndexOutOfBounds error?

    If there is an error, shouldn't you add try catch blocks to deal with it?

    If you think about it, why even store the values in a list at all, when you can store the indices and just reference them from splitField?

    The list goes on.

    As you can see, I get butthurt when people nitpick my shit :) But if you are going to do it, go all the way. I was just trying to give homeboy the idea. But if you want production ready code, by all means, provide it.
     
    robhustle, Oct 24, 2008 IP
  5. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    My bad starlight. I'm having a shitty day. Didn't mean to snap at you bro.
     
    robhustle, Oct 24, 2008 IP
  6. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey, we're cool. I appreciate it though. I only mentioned those things because they said they were new. Sometimes new people get lost in syntax and miss the broader concept. But I know what you mean about the day. Mine was pretty crappy too. TGIF is all I can say..
     
    cfStarlight, Oct 24, 2008 IP
  7. jessegifford

    jessegifford Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the help. I decided to go with the list solution and that did the trick.

    Thanks again,
    Jesse
     
    jessegifford, Oct 30, 2008 IP