StructCopy

Creates a shallow copy of a struct.

Copies top-level keys, values, and arrays in the structure by value; copies nested structures by reference.

Method Signature

StructCopy(struct=[structloose])

Arguments

Argument
Type
Required
Description
Default

struct

struct

true

The struct to copy

Examples

Copy a structure and change it. Original structure stays unchanged

Run Example

myStruct = { 
	"a" : 1,
	"b" : 2,
	"c" : 3
};
myNewStruct = structCopy( myStruct );
myNewStruct.B = 5;
myNewStruct[ "d" ] = 4;
structDelete( myNewStruct, "c" );
writeOutput( structKeyList( myStruct ) & " → " & structKeyList( myNewStruct ) );

Result: b,a,c → b,a,d

Additional Examples

Run Example

animals = { 
	COW : "moo",
	PIG : "oink"
};
// Show current animals
Dump( label="Current animals", var=animals );
// Copy animals struct to farm
farm = StructCopy( animals );
// Show farm, looks like animals
Dump( label="Farm after StructCopy()", var=farm );
// Add another animal. Will not affect farm.
StructAppend( animals, {
	CAT : "meow"
} );
// Show animals, now includes cat
Dump( label="Animals with cat added", var=animals );
// Show farm, does not have cat
Dump( label="Farm copied from animals before cat was added", var=farm );

Last updated

Was this helpful?