StructInsert

Inserts a key/value pair in to a struct - with an optional overwrite argument

Method Signature

StructInsert(struct=[modifiableStruct], key=[any], value=[any], overwrite=[boolean])

Arguments

Argument
Type
Required
Description
Default

struct

modifiableStruct

true

The target struct

key

any

true

The struct key

value

any

true

The value to assign for the specified key

overwrite

boolean

false

Whether to overwrite the existing value if the key exists ( default: false )

false

Examples

Simple Example

Inserts a new key/value into the structure

Run Example

map = { 
	"hair" : "brown",
	"eyes" : "green"
};
structInsert( map, "lips", "red" );
writeOutput( structKeyList( map ) );

Result: eyes,lips,hair

Overwrite Example

Throws exception when you try to add a duplicate key, when allowoverwrite parameter is false.

Run Example

map = { 
	"hair" : "brown",
	"eyes" : "green"
};
try {
	structInsert( map, "hair", "red", false );
} catch (any ex) {
	writeOutput( "error!" );
}

Result: error!

Additional Examples

Run Example

animals = { 
	COW : "moo",
	PIG : "oink"
};
// Show current animals
Dump( label="Current animals", var=animals );
// Insert cat into animals
StructInsert( animals, "cat", "meow" );
// Show animals, now includes cat
Dump( label="Animals with cat added", var=animals );

Last updated

Was this helpful?