S-Lang function to duplicate a structured variable.
Struct_Type dup_struct( Struct_Type s )
Struct_Type dup_struct( Struct_Type s, selectedfields )
A convenience function for duplicating a structured variable. A
slice, i.e. some potentially reordered subset of fields, may
optionally be created from the structure by specifying one of
the following as the second parameter:
-
a comma-delimited list of field names to include
-
a similar list, but beginning with a single "!", for exclusion
-
an integer representing the first N fields
-
NULL, in which case only non-NULL fields in the input structure
will be copied
Given this structured type definition and variable instance,
chips> typedef struct {first, middle, last} Name_Type;
chips> variable mozart = @Name_Type;
chips> mozart.first = "Wolfgang";
chips> mozart.middle = "Amadeus";
chips> mozart.last = "Mozart";
this will make an entire copy of the structure:
chips> variable copy = dup_struct(mozart);
while the following is an example of one kind of slice:
chips> variable slice = dup_struct(mozart,"!first");
chips> print(slice);
middle = Amadeus
last = Mozart
Structures can contain NULL fields:
chips> s = @struct{ elem1, elem2, elem3 };
chips> s.elem1 = "hello"
chips> s.elem3 = "there!"
chips> print(s)
elem1 = hello
elem2 = NULL
elem3 = there!
If NULL is supplied as the second parameter to dup_struct(),
then these NULL fields - elem2 in this example - will not be copied
(they are by default):
chips> copy = dup_struct(s,NULL)
chips> print(copy)
elem1 = hello
elem3 = there!
|