INSERT
INSERT a, idx, val [, val [, …]]]
Inserts the values to the specified array at the position idx.
- a - An array-variable.
- idx - Position in the array.
- val - Any value or expression.
Example 1: Insert one variable
1,2,3,4,5]
a = [
print a
insert a, 3, 6
print a
' Output:
' [1,2,3,4,5]
' [1,2,3,6,4,5]
Example 2: Insert several variables
1,2,3,4,5]
a = [
print a
insert a, 3, 6, 7, 8
print a
' Output:
' [1,2,3,4,5]
' [1,2,3,8,7,6,4,5]
Please be aware, that multiple variables will be sequentially inserted each at position 3. This leads to a reverse order.
Example 3: Insert an array
1,2,3,4,5]
a = [
print a
insert a, 3, [6,7]
print a
' Output:
' [1,2,3,4,5]
' [1,2,3,[6,7],4,5]
Example 4: Insert a map variable
1,2,3,4,5]
a = [6, y:7}
b = {x:
print a
insert a, 3, b
print a
' Output:
' [1,2,3,4,5]
' [1,2,3,{"y":7, "x":6},4,5]
Example 5: Append a variable (insert at last position)
1,2,3,4,5]
a = [
print a
6
a << print a
' Output:
' [1,2,3,4,5]
' [1,2,3,4,5,6]
Code samples using INSERT