Differences in Creating Copy of a Structure Using CFSet, Duplicate, and Structcopy in ColdFusion

AID: 8971
  • Status: Published

2490 points

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after reading, you should get a clear understanding of what each does and which one is best in a given scenario.

Using CFSet

Creating a copy of a Structure using CFSet will result in a new variable having a reference to the original Structure; therefore, changes will affect the original Structure.

Example:
<cfset structtest = structnew()>
<cfset structtest.txt = "Testing">
<cfset structtest.nestedstruc = structnew()>
<cfset structtest.nestedstruc[1] = "sri">
<cfset structtest.nestedstruc[2] = "Kanth">
<cfset Variables.st1 = structtest>

<cfdump var="#structtest#" label="original structtest Structure">

<cfdump var="#st1#" label="st1 struct - using Cfset">

<cfset st1.txt = "Testing1">
<cfset st1.nestedstruc[2] = "Kanth M">
<h3> After assigning Values </h3>
<cfdump var="#structtest#" label="original Session Structure">

<cfdump var="#st1#" label="st1 struct - using Cfset">
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window


 
cfset.JPG
  • 37 KB
  • Using CFSet
Using CFSet

In the above code, we have a Structure structtest with a simple variable txt and nested Structure nestedstruc.
Since st1 references structtest, we can see that values updated for txt and nestedstruc of st1 are also reflected in the original Structure structtest.

Using Duplicate

Creating a copy of a Structure using Duplicate will result in a new variable containing no reference to the original Structure but rather a snapshot of its current contents.

Example:
<cfset structtest = structnew()>
<cfset structtest.txt = "Testing">
<cfset structtest.nestedstruc = structnew()>
<cfset structtest.nestedstruc[1] = "sri">
<cfset structtest.nestedstruc[2] = "Kanth">
<cfset Variables.st1 = duplicate(structtest)>

<cfdump var="#structtest#" label="original structtest Structure">

<cfdump var="#st1#" label="st1 struct - using Duplicate">

<cfset st1.txt = "Testing1">
<cfset st1.nestedstruc[2] = "Kanth M">
<h3> After assigning Values </h3>
<cfdump var="#structtest#" label="original Session Structure">

<cfdump var="#st1#" label="st1 struct - using Duplicate">
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window


 
duplicate.JPG
  • 40 KB
  • Using Duplicate
Using Duplicate

In the above sample, we can see that the values updated for txt and nestedstruc of st1 are not reflected in the original Structure structtest.

Using Strcutcopy

Strcutcopy is where most people get confused.
We can say Structcopy is between CFSet and Duplicate.
When we use Structcopy, a new structure will be created; however, references to nested structures, objects, and query variables will be maintained from the original Structure.

Example:
<cfset structtest = structnew()>
<cfset structtest.txt = "Testing">
<cfset structtest.nestedstruc = structnew()>
<cfset structtest.nestedstruc[1] = "sri">
<cfset structtest.nestedstruc[2] = "Kanth">
<cfset Variables.st1 = structcopy(structtest)>

<cfdump var="#structtest#" label="original structtest Structure">

<cfdump var="#st1#" label="st1 struct - using structcopy">

<cfset st1.txt = "Testing1">
<cfset st1.nestedstruc[2] = "Kanth M">
<h3> After assigning Values </h3>
<cfdump var="#structtest#" label="original Session Structure">

<cfdump var="#st1#" label="st1 struct - using structcopy">
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window


 
structcopy.JPG
  • 39 KB
  • Using Structcopy
Using Structcopy

In the above example, we can see that the change for txt is not reflected in the original Structure, but the "2" key change of the nested nestedstruc Structure is reflected in the structtest Structure .

Conclusion

Try to use the best one while copying a Structure which will be more effective for your scenario:
Cfset – Create a reference to the original Structure.
Duplicate – Create a new Structure with no reference to the original Structure.
Structcopy – Create a new Structure with references to the complex variables contained in the original Structure like nested structures, objects, and query variables.
    Asked On
    2011-12-28 at 01:05:36ID8971
    Tags

    coldfusion

    ,

    structcopy

    ,

    duplicate

    ,

    cfset

    Topic

    ColdFusion Application Server

    Views
    1970

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top ColdFusion Server Experts

    1. Zvonko

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    2. srikanthmadishetti

      2,580

      20 points yesterday

      Profile
      Rank: Guru
    3. digicidal

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    4. maestropsm

      1,600

      0 points yesterday

      Profile
      Rank: Guru
    5. _agx_

      1,268

      0 points yesterday

      Profile
      Rank: Genius
    6. gdemaria

      1,064

      0 points yesterday

      Profile
      Rank: Genius
    7. brijeshchauhan

      668

      0 points yesterday

      Profile
      Rank: Guru
    8. myselfrandhawa

      250

      10 points yesterday

      Profile
      Rank: Guru

    Hall Of Fame