URScript examples for manipulating structs and lists
Only applicable for UR20/UR30
Software release 5.15.0 introduces additions to URScript aimed to improve handling of complex data sets. New in this release are structs, lists with variable length and methods for manipulating structs and lists.
Contents
New features in URSCript for SW release 5.15
Support for structs (complex data types)
Support for structs (complex data types)
In URScript, a set of variables can be aggregated into struct, and thus transferred and stored as a single variable.
Structs can be obtained through multiple means:
- Using the struct() function
- Executing an RPC call that returns a struct
The struct function takes one or more named arguments, and each argument name becomes a member in the struct. All values must be initialized by value, and the type of the value cannot be changed subsequently.
Create a struct:
myStruct = struct(identifier1 = 1, identifier2 = 2, myMember = "Hello structs", listMember = [1,2,3])
Use a member:
myVar = myStruct.myMember
Variable length lists
A list object in URScript has two attributes: length and capacity. The length indicates how many elements the list currently holding. The capacity tells how many elements the list can hold maximum. Once declared, the capacity of the list cannot be changed.
Variable length lists can be created using new make_list() function:
bb = make_list(length = 7, initial_value = 11, capacity = 20)
Where length of bb is 7, but capacity is 20, and list can hold numbers. List can be extended and contracted between 0, and 20 numeric elements.
Variable length lists are fully backwards compatible with fixed length lists. List created with [...] has capacity equal to length.
aa = [1, 2, 3.5, 4, 5.5]
Where aa has both length and capacity equal to 5.
Lists can hold any type that URScript supports. This includes complex values created with struct() keyword:
aa = [1, 2, 3.5, 4, 5.5]
bb = make_list(10, struct(p1 = 1, p2 = "text"), 10)
cc = ["a", "b", "c", "d"]
dd = [struct(m1 = 10, m2 = "hello", m3 = make_list(length = 25, initial_value = 0, capacity = 100)) , struct(m1 = 20, m2 = "hi", m3 = make_list(length = 50, initial_value = 0, capacity = 100))]
List can be assigned only to existing list of greater or equal capacity to the length of source list:
aa = [1, 2, 3, 4, 5, 6] # aa.length() == 6, aa.capacity() == 6
bb = make_list(5, 0, 100) # bb.length() == 5, bb.capacity() == 100
aa = bb # aa capacity will remain 6 aa length will be 5
aa = [1, 2, 3, 4, 5, 6]
bb = aa # bb capacity will remain 100 bb length will be 6
List can hold structs (aka complex data types). All structs in the list have to be exactly of the same type:
aa = make_list(10, struct(p1 = 1, p2 = "text"), 10)
a = aa[4].p1 # a = 1
b = aa[4].p2 # b = "text"
aa[3].p1 = 22.5
aa[4] = struct(p1 = 99, p2 = "different text")
Methods for list manipulation
Methods (member functions) on lists can be called by using the "." followed by the function name:
append(element)
Adds the element to the end of the list. Raises an error if at capacity.
Example: add the value 88 to a list.
l1 = make_list(0, 0, 10) # empty list of integers with capacity of 10
l1.append(88) # add element to the end of the list, length increases, exception thrown if capacity exceeded
capacity()
Returns the maximum capacity of the list (>=length).
Example: merge list 2 to list 1 until list 1 is full. result: [-1, -1, -1, -1, -1, 6, 7, 8, 9, 10]
l1 = make_list(5, -1, 10)
l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
idx = l1.length()
while(idx < l1.capacity()):
l1.append(l2[idx])
idx = idx + 1
end
clear()
Clear the list by setting length to 0.
list_1.clear() # list_1 will be []
excess_capacity()
Returns the unused capacity (= capacity-length).
Example: add element if the list has free space. result: [9,9,9,9,9,1,2,3,4,5]; popup "no more space"
l1 = make_list(5, 9, 10)
l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
idx = 0
while(idx < l2.length()):
if(l1.excess_capacity() > 0):
l1.append(l2[idx])
else:
popup("no more space")
break
end
idx = idx + 1
end
extend(list of elements)
Adds all elements from the parameter list at the end. Raises an error if at capacity. The list in the input must be of the same type as the list.
Example: add list 2 to list 1. result = [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l1 = make_list(2, 0, 100)
l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if l1.excess_capacity() > l2.length(): l1.extend(l2) end
insert(index, element)
Inserts the element at the given index, shifting remaining list elements. Raises an error if at capacity.
length()
Returns the current length of the list.
Example: update elements of a list in a loop
l1 = [1, 2, 3, 4, 5, 6]
idx = 0
while (idx < l1.length()):
l1[idx] = 10 + idx
idx = idx + 1
end
pop()
Removes the last element from the list.
remove(index)
Removes the element at a given index.
Example: remove even numbers from a collection.
l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
idx = l2.length() - 1
while(idx > 0):
if(l2[idx] % 2 == 0):
l2.remove(idx)
end
idx = idx - 1
end
slice(begin index, end index)
Returns a sub-list of elements [list[param1]... list[param2-1]]. Does not modify the original list.
to_string()
Returns a string representation of the list. has ...] if out of space.
Methods on structs
Methods (member functions) on structs can be called by using the "." followed by the function name:
length() Returns the number of elements in the struct.
to_string() Returns a string representation of the struct. has "...}" if we run out of view space.
Using structs as request in RPC calls
Structs can be used as a parameter to RPC calls.
Methods on matrices
Methods (member functions) on matrices can be called by using the "." followed by the function name:
shape() Returns array with sizes of each matrix dimension.
m = [[1,2],[3,4],[5,6]]
s = m.shape() # s = [3, 2]
get_column(index) Returns selected matrix column. First column has index 0.
get_row(index) Returns selected matrix row. First row has index 0.
to_string() Returns a string representation of the struct. has "...}" if we run out of view space.