![]() |
|
Navigation |
Synopsis Assign to a slice of a list or string.
Syntax
Exp1 and Exp3 are optional
Description A slice assignment is defined for List, String and Node and aims to replace a slice from the old value of the assignable by a new value. See Values/List/Slice, String/Slice or Node/Slice for a more detailed explanation of slicing.
Let V be the current value of Assignable .
Examples Replace the elements with index 3, 4, 5 in
L :
rascal>L = [0,1,2,3,4,5,6,7,8,9]; list[int]: [0,1,2,3,4,5,6,7,8,9] rascal>L[3..6] = [100,200,300,400,500]; list[int]: [0,1,2,100,200,300,400,500,6,7,8,9]Replace the elements with index 1, 3, 5, 7 in L (note how the elements from [100,200] are used in a circular way):
rascal>L = [0,1,2,3,4,5,6,7,8,9]; list[int]: [0,1,2,3,4,5,6,7,8,9] rascal>L[1,3..8] = [100,200]; list[int]: [0,100,2,200,4,100,6,200,8,9]Replace the elements with index 1, 3, 5, 7 in L (note how the unused elements from [100,200,300,400,500]
are insert at index 7):
rascal>L = [0,1,2,3,4,5,6,7,8,9]; list[int]: [0,1,2,3,4,5,6,7,8,9] rascal>L[1,3..8] = [100,200,300,400,500]; list[int]: [0,100,2,200,4,300,6,400,500,8,9]Similar examples for slicing assignment on strings: rascal>S = "abcdefghij"; str: "abcdefghij" rascal>S[3..6] = "UVWXYZ"; str: "abcUVWXYZghij" rascal>S = "abcdefghij"; str: "abcdefghij" rascal>S[1,3..8] = "XY"; str: "aXcYeXgYij" rascal>S = "abcdefghij"; str: "abcdefghij" rascal>S[1,3..8] = "UVWXYZ"; str: "aUcVeWgXYZij" rascal> cancelledReplace the elements with index 3, 4, 5 in node N :
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999}); node: "f"( 0, true, 2, "abc", 4, 5.5, 6, {7,77}, 8, {99,999,9}) rascal>N[3..6] = [100,200,300,400,500]; node: "f"( 0, true, 2, 100, 200, 300, 400, 500, 6, {7,77}, 8, {99,999,9})Replace the elements with index 1, 3, 5, 7 in L (note how the elements from [100,200] are used in a circular way):
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999}); node: "f"( 0, true, 2, "abc", 4, 5.5, 6, {7,77}, 8, {99,999,9}) rascal>N[1,3..8] = [100,200]; node: "f"( 0, 100, 2, 200, 4, 100, 6, 200, 8, {99,999,9})Replace the elements with index 1, 3, 5, 7 in L (note how the unused elements from [100,200,300,400,500]
are insert at index 7):
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999}); node: "f"( 0, true, 2, "abc", 4, 5.5, 6, {7,77}, 8, {99,999,9}) rascal>N[1,3..8] = [100,200,300,400,500]; node: "f"( 0, 100, 2, 200, 4, 300, 6, 400, 500, 8, {99,999,9}) ![]() |