This type provides the core map class for Boxlang.
Structs are highly versatile and are used for organizing and managing related data.
Types of Structs in BoxLang:
Basic Structs: These are the basic structures where each key is associated with a single value. Keys are case-insensitive and can be strings or symbols.
Nested Structs: Structs can contain other structs as values, allowing for a hierarchical organization of data.
Case-Sensitive Structs: By default, BoxLang structs are case-insensitive. However, you can create case-sensitive structs if needed.
Ordered Structs: This implementation of a Struct maintains keys in the order they were added.
Sorted Structs: This implementation of a Struct maintains keys in specified sorted order.
Examples
Creating structs using the function structNew
// Create a default struct ( unordered )myStruct =structNew();// Create an ordered struct which will maintain key order of insertionmyStruct =structNew( "ordered" );// Create a case-sensitive struct which will require key access to use the exact casingmyStruct =structNew( "casesenstive" );myStruct[ "cat" ] ="pet";myStruct[ "Cat" ] ="wild";// Create a sorted struct myStruct =structNew( "sorted","textAsc" )
Creating structs using object literal syntax
// Create an empty default struct ( unordered )myStruct = {};// Create an empty struct and populate it with valuesanimals = { cow:"moo", pig:"oink"};// Create an ordered struct which will maintain key order of insertion// Note that you must provide the ordered struct with data to prevent confusion as to whether it is an array or structorderedAnimals = [ cow:"moo", pig:"oink"];
Appends the contents of a second struct to the first struct either with or without overwrite
Arguments:
Examples: Append One Struct to Another:
animals = { cow:"moo", pig:"oink"};// Show current animalsanimals.dump( label ="Current animals" );// Create a new animalnewAnimal = { cat:"meow"};// Append the newAnimal to animalsanimals.append( newAnimal );animals.dump( label="Updated animals" );
clear()
Clear all items from struct
copy()
Creates a shallow copy of a struct.
Copies top-level keys, values, and arrays in the structure by value; copies nested structures by reference.