Structure operations
Contents: Description, Procedure, Functions Used, Related Functions
Description Structures in davinci are polymorphus containers that can have any number of elements of any data type including other structures. A good way to think about a structure is to imagine it as an empty box that swells to whatever size needed to hold the things you put in it. The box itself has no real properties other than that it contains things within it. You can put any davinci object into a structure; strings, numeric arrays, numbers, or even other structures all may be put in a structure for organizational purposes and are called 'elements' of that structure. To accomplish work with data in a structure you must do something to the element corresponding to the data you want. Following the analogy, if you wanted to paste some pictures into a scrapbook that is in a cedar-chest you must reach into the cedar-chest to get at the scrapbook, you can't simply paste the pictures onto the outside of the cedar-chest. The cedar-chest has nothing directly to do with the pictures, it simply is a convenient storage device for the damn scrapbook. Below are some procedures (both common and uncommon) and uses of structures.
Procedure Basic usage of structures: Create an empty structure, two equivalent examples: dv> struc = {} struct, 0 elements dv> struc = struct() struct, 0 elements Adding an element to the structure when you know its name and data type: dv> my_array 10x10x10 array of float, bsq format [4,000 bytes] dv> struc.data = my_array 10x10x10 array of float, bsq format [4,000 bytes] dv> struc struct, 1 elements data: 10x10x10 array of float, bsq format [4,000 bytes] dv> my_text = "this is text describing my structure" "this is text describing my structure" dv> struc.label = my_text "this is text describing my structure" dv> struc struct, 2 elements data: 10x10x10 array of float, bsq format [4,000 bytes] label: "this is text describing my structure" Every structure element has an 'index' and may have a 'key'. Keys are just names of the elements. In the above case struc has two keys, data and label. The indices are designated by order of creation, so data has an index of 1 and label has an index of 2. A structure element may be accessed by either its index or its key: dv>struc[1] 10x10x10 array of float, bsq format [4,000 bytes] dv>struc.data 10x10x10 array of float, bsq format [4,000 bytes] You may add named or unnamed elements to a structure using the function insert_struct() or add_struct(): dv>insert_struct(struc,after=2,value=5.6667) 5.66670 dv> struc struct, 3 elements data: 10x10x10 array of float, bsq format [4,000 bytes] label: "this is text describing my structure" : 5.66670 dv> add_struct(struc,"another description") "another description" dv> struc struct, 4 elements data: 10x10x10 array of float, bsq format [4,000 bytes] label: "this is text describing my structure" : 5.66670 : "another description" If you are given a structure and you want to know how many elements it has without counting them by hand, use the function length(): dv>len = length(struc) 4 You can loop through the elements of the structure by using the indices: dv>for(i=1;i<=len;i+=1) { 1>t = get_struct_key(struc,i) 1>printf(t+"\n") 1>} data label Advanced usage of structures: Elements may not have keys starting with numbers. This causes some problems with creating a structure that gains elements in a script where you want them to be sequentially named. (e.g. a struct S that contains elements that have the same key as their index) Using the functions eval() and sprintf() gives a work-around. dv> struc_2 = {} struct, 0 elements dv> for(i=1;i<=7;i+=1) { 1> eval(sprintf("elem%.2d",i)+"=i") 1> add_struct(struc_2,eval(sprintf("elem%.2d",i))) 1> } dv> struc_2 struct, 7 elements elem01: 1 elem02: 2 elem03: 3 elem04: 4 elem05: 5 elem06: 6 elem07: 7
|
DavinciWiki Mini-Nav Bar Contents
Contact Developers
All other topics
Functions Used
Related Procedures |