Navigation
Synopsis Assign to a slice of a list or string.
Syntax
  1. Assignable [ Exp1 .. Exp3 ] = Exp4
  2. Assignable [ Exp1, Exp2 .. Exp3 ] = Exp4
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.
  1. Assignable [ Exp1 .. Exp3 ] = Exp4: The slice [ Exp1 .. Exp3 ] determines two indices begin (inclusive) and end (exclusive) in V. A new value V' is computed that is a copy of V but with all the elements in V with begin <= index < end replaced by the elements of the value of Exp4. Note that the size of V and V' may differ. V' is assigned to the Assignable.
  2. Assignable [ Exp1, Exp2 .. Exp3 ] = Exp4: The slice [ Exp1, Exp2 .. $Exp_3 ] determines two indices begin (inclusive) and end (exclusive) and a step between indices in V. A new value V' is computed that is a copy of V but with all the elements in V with indices begin, begin+step. ... end-step <= index < end replaced by the successive elements of the value of Exp4. Note that the size of V and V' may differ. V' is assigned to the Assignable.

    If the number of indices in the slice and the number of elements in the value of Exp4 is not equal the following is done:
    • If the number of elements in the slice is larger: the elements of Exp4 are used in a circular manner.
    • If the number of elements in the slice is smaller: the remaining elements of Exp4 is inserted after the last index in the slice.
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>
cancelled
Replace 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})
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.