STRUCTCOPY  
説明

構造体をコピーします。トップレベルのキー、値、および配列は値がコピーされます。ネストされた構造体は参照がコピーされます。

 
戻り値

同じキーと値を持つ構造体のコピー。structure が存在しない場合は例外が発生します。

 
カテゴリ

構造体関数

 
関数のシンタックス
StructCopy(structure)
 
関連項目

構造体関数、『ColdFusion MX 開発ガイド』の第5章の「配列と構造体の使用」 の「構造体関数」

 
パラメータ

 
使用方法

文字フィールド、数値フィールド、および 2 次元配列をトップレベルに持つ構造体を、この関数でコピーすると、次のコードと同様の処理が行われます。

<cfoutput>
   <cfset assignedCopy = StructNew()>
  <cfset assignedCopy.string = #struct.string#>
   <cfset assignedCopy.number = #struct.number#>
   <cfset assignedCopy.array = ArrayNew(2)>
   <cfset assignedCopy.array[1][1] = #struct.array[1][1]#>
   <cfset assignedCopy.array[1][2] = #sruct.array[1][2]#>
</cfoutput>

ネストされた構造体を StructCopy でコピーすると、次のコードと同様の処理が行われます。

<cfoutput>
  <cfset assignedCopy.nestedStruct = struct.nestedStruct>
</cfoutput>

構造体全体を値でコピーするには、141ページのDuplicate を使用します。

次の表に、各種の変数が代入される方法を示します。

   
structure.any_simple_value (単純値)
ブール値
バイナリ
Base64
structure.array (配列)
structure.nested_structure (ネストされ た構造体)
参照
structure.object (オブジェクト)
参照
structure.query (クエリー)
参照

 
<!--- このコードは、値による代入と参照による代入を示したものです。 --->
// このスクリプトでは、StructCopy でのコピーが値によって行われる構造体を作成します。<br> 
<cfscript>
   // 要素を作成します。
   s = StructNew();
   s.array = ArrayNew(2);

   // コピー元の構造体のトップレベルのフィールドに単純値を代入します。
   s.number = 99;
   s.string = "hello tommy";

   // コピー元の構造体のトップレベルの配列に値を代入します。
   s.array[1][1] = "one one";
   s.array[1][2] = "one two";
</cfscript>

<!--- コピー元の構造体を出力 --->
<hr>
<b>コピー元の構造体の値</b><br>
<cfoutput>
   // 単純値 <br>
   s.number = #s.number#<br>
   s.string = #s.string#<br>
   // 配列値 <br>
   s.array[1][1] = #s.array[1][1]#<br>
   s.array[1][2] = #s.array[1][2]#<br>
</cfoutput>

// この構造体を新しい構造体にコピーします。<br>
<cfset copied = StructCopy(s)>

<cfscript>
// コピー元の構造体の値を変更します。<br>
   s.number = 100;
   s.string = "hello tommy (modified)";
   s.array[1][1] = "one one (modified)";
   s.array[1][2] = "one two (modified)";
</cfscript>
<hr>
<b>コピー元構造体の変更後の値</b><br>
<cfoutput>
   // 単純値 <br>
   s.number = #s.number#<br>
   s.string = #s.string#<br>
   // 配列値 <br>
   s.array[1][1] = #s.array[1][1]#<br>
   s.array[1][2] = #s.array[1][2]#<br>
</cfoutput>
<hr>
<b>コピー先の構造体の値は元のままです。</b><br>
<cfoutput>
   // 単純値 <br>
   copied.number = #copied.number#<br>
   copied.string = #copied.string#<br>
   // 配列値 <br>
   copied.array[1][1] = #copied.array[1][1]#<br>
   copied.array[1][2] = #copied.array[1][2]#<br>
</cfoutput>

// このスクリプトでは、StructCopy でのコピーが参照によって行われる構造体を作成します。 
<cfscript>
   // 要素を作成します。
   s = StructNew();
   s.nested = StructNew();
   s.nested.array = ArrayNew(2);
   // ネストされた構造体のフィールドに単純値を代入します。
   s.nested.number = 99;
   s.nested.string = "hello tommy";
   // ネストされた配列に値を代入します。
   s.nested.array[1][1] = "one one";
   s.nested.array[1][2] = "one two";
</cfscript>

<!--- コピー元の構造体を出力 --->
<hr>
<b>コピー元の構造体の値</b><br>
<cfoutput>
   // 単純値 <br>
   s.nested.number = #s.nested.number#<br>
   s.nested.string = #s.nested.string#<br>

   // 配列値 <br>
   s.nested.array[1][1] = #s.nested.array[1][1]#<br>
   s.nested.array[1][2] = #s.nested.array[1][2]#<br>
</cfoutput>

// StructCopy を使用して、この構造体を新しい構造体にコピーします。<br>
<cfset copied = StructCopy(s)>
// Duplicate を使用して、この構造体のクローンである新しい構造体を作成します。<br>
<cfset duplicated = Duplicate(s)>

<cfscript>
// コピー元の構造体の値を変更します。
   s.nested.number = 100;
   s.nested.string = "hello tommy (modified)";
   s.nested.array[1][1] = "one one (modified)";
   s.nested.array[1][2] = "one two (modified)";
</cfscript>
<hr>
<b>コピー元構造体の変更後の値</b><br>
<cfoutput>
   // 単純値 <br>
   s.nested.number = #s.nested.number#<br>
   s.nested.string = #s.nested.string#<br>

   // 配列値 <br>
   s.nested.array[1][1] = #s.nested.array[1][1]#<br>
   s.nested.array[1][2] = #s.nested.array[1][2]#<br>
</cfoutput>

<hr>
<b>元の構造体の値が変更されると、コピー先の構造体にも反映されます。</b><br>
<cfoutput>
   // 単純値 <br>
   copied.nested.number = #copied.nested.number#<br>
   copied.nested.string = #copied.nested.string#<br>
   // 配列値 <br>
   copied.nested.array[1][1] = #copied.nested.array[1][1]#<br>
   copied.nested.array[1][2] = #copied.nested.array[1][2]#<br>
</cfoutput>

<hr>
<b>Duplicate で作成したクローン構造体の値は元のままです。</b><br>
<cfoutput>
   // 単純値 <br>
   duplicated.nested.number = #duplicated.nested.number#<br>
   duplicated.nested.string = #duplicated.nested.string#<br>
   // 配列値 <br>
   duplicated.nested.array[1][1] = #duplicated.nested.array[1][1]#<br>
   duplicated.nested.array[1][2] = #duplicated.nested.array[1][2]#<br>
</cfoutput>
STRUCTURE  
コピーする構造体です。