Coldfusion Struct, when copied:

<cfset new1 = StructNew() />
<cfset new2 = StructNew() />

<cfset new1 = new2 />

Is just a pointer in the system (documented too). It does not have any data that is by its own, so when you update new2, new1 will be updated. To avoid this, use StructCopy().


On 1/7/2014 12:25 PM, Alan Holden wrote:
I'm glad that worked for you.

Virtually every CFML engine I've used has given me random issues like this one : object elements persisting inside loops.

I've come to explicitly cast the "index" element of (whatever complex thing I'm looping through) into its own variable right at the top of any loop, by habit now.

Al

On 1/6/2014 10:38 PM, Magnus wrote:
Well, the docs are pretty clear that you can create a struct implicitly by using struct dot notation... Ah but perhaps I need to be explicit about it being a new struct each time. Wait... let's try this.

Ok. that is it. Now it works. I just added "<cfset fileInfo=strcutNew()>" just inside the loop and it works as expected.

I wasearlier looking at a similar example I did a while back but couldn't see the difference (assuming implicit creation was the same as explicit) but in that case I used the full formal syntax for declaring and creating structs including declaring a new struct.

So an easy fix. Thanks.

On Monday, 6 January 2014 20:45:57 UTC-8, Al Holden wrote:

    oops. As a new struct, not a new array.

    On 1/6/2014 8:24 PM, Alan Holden wrote:
    Where is fileInfo declared as a new array?
    Shouldn't that be the first step inside your loop?
    Just curious.
    Al

    P.S. Just for debugging fun, add i as an array element too.


    On 1/6/2014 7:48 PM, Magnus wrote:
    I am trying to create an array of structs from values passed
    from a form. Using this code, the resulting array has the same
    value in each array cell from last iteration of the loop. The
    desired result, of course, is toi hae each pair of form values
    passed to a struct in each array cell. As far as I can tell, it
    seems the earsh array cell is only a reference to the fileInfo
    struct which gets rewritten each loop. I can't recall where I
    have read about assigning vallues and referencing variables in
    CFML to help me clarify wht is going on and how to get it
    working as I want. (If you are wondering, the form is from
    Plupload for uploading files.

    I want ot put the valuesinto an array because I will be doing a
    bunch of operations to or on the files and looping over and
    array each time will be simplier can repeatedly dealing with
    the dynamically generated form variables. Any help getting on
    track and beter understanding what is goingon here is much
    appeciated.

    Magnus


    <cfdump var="#FORM#"> ...the starting values...
    struct
    FIELDNAMES
    
UPLOADER_0_NAME,UPLOADER_0_STATUS,UPLOADER_1_NAME,UPLOADER_1_STATUS,UPLOADER_2_NAME,UPLOADER_2_STATUS,UPLOADER_3_NAME,UPLOADER_3_STATUS,UPLOADER_4_NAME,UPLOADER_4_STATUS,UPLOADER_COUNT

    UPLOADER_0_NAME     ANGELS.wav
    UPLOADER_0_STATUS    done
    UPLOADER_1_NAME     Kalimba.mp3
    UPLOADER_1_STATUS
        done
    UPLOADER_2_NAME     Machine_Gun_Alley_1.ogg
    UPLOADER_2_STATUS
        done
    UPLOADER_3_NAME     Maid with the Flaxen Hair.mp3
    UPLOADER_3_STATUS    done
    UPLOADER_4_NAME     Sleep Away.mp3
    UPLOADER_4_STATUS
        done
    UPLOADER_COUNT      5


    Here is the code..
    <cfset fileArray = arrayNew(1)>
    <cfloop index="i" from="0" to="#FORM.uploader_count-1#" step="1">
        <cfset fileInfo.name=#Evaluate("FORM.uploader_" & i &
    "_name")#>
        <cfset fileInfo.status=#Evaluate("FORM.uploader_" & i &
    "_status")#>
        <cfset fileArray[i+1] = fileInfo>
        <cfdump var="#fileInfo#"

    </cfloop>

    <cfdump var="#fileArray#"> ...the end result...
    array
    1
        
    struct
    name        Sleep Away.mp3
    status       done

    2   
    struct
    name        Sleep Away.mp3
    status
        done

    3   
    struct
    name        Sleep Away.mp3
    status       done

    4   
    struct
    name        Sleep Away.mp3
    status       done

    5   
    struct
    name        Sleep Away.mp3
    status       done


-- -- online documentation: http://openbd.org/manual/
    http://groups.google.com/group/openbd?hl=en
    <http://groups.google.com/group/openbd?hl=en>

    ---
    You received this message because you are subscribed to the
    Google Groups "Open BlueDragon" group.
    To unsubscribe from this group and stop receiving emails from
    it, send an email to [email protected] <javascript:>.
    For more options, visit
    https://groups.google.com/groups/opt_out
    <https://groups.google.com/groups/opt_out>.

-- -- online documentation: http://openbd.org/manual/
    http://groups.google.com/group/openbd?hl=en
    <http://groups.google.com/group/openbd?hl=en>

    ---
    You received this message because you are subscribed to the
    Google Groups "Open BlueDragon" group.
    To unsubscribe from this group and stop receiving emails from
    it, send an email to [email protected] <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out
    <https://groups.google.com/groups/opt_out>.

--
--
online documentation: http://openbd.org/manual/
http://groups.google.com/group/openbd?hl=en

---
You received this message because you are subscribed to the Google Groups "Open BlueDragon" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

--
--
online documentation: http://openbd.org/manual/
http://groups.google.com/group/openbd?hl=en

---
You received this message because you are subscribed to the Google Groups "Open BlueDragon" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

--
--
online documentation: http://openbd.org/manual/
http://groups.google.com/group/openbd?hl=en

--- You received this message because you are subscribed to the Google Groups "Open BlueDragon" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to