Contents

Roo

Array.slice!()

There are two slice!() methods:

First form

array.slice!(index as Number) as (Object or Nothing)

Returns the element at the specified index, removing it from this array. A negative index is permitted where -1 is the last element.

Parameters

  • index. Integer Number. The index of the element to retrieve. Negative indices count backwards from the end of the array (-1 is the last element).

Returns

  • The element at the specified index. If the absolute value of index is greater than array.length then the method returns Nothing. The element is removed from this array.

Example

var a = ["a", "b", "c"]
var b = a.slice!(0)
print(a) # => ["b", "c"]
print(b) # => "a"

var c = ["a", "b", "c"]
var d = c.slice!(3)
print(c) # => ["a", "b", "c"]
print(d) # => Nothing

var e = ["a", "b", "c"]
var f = e.slice!(-1)

print(e) # => ["a", "b"]
print(f) # => "c"

Second form

array.slice!(start as Number, length as Number) as (Array or Nothing)

Returns a new array formed from the elements of this array beginning at index start, for length elements. If start is negative, we count backwards from the end of the array (-1 is the last element). The elements returned are removed from this array.

Parameters

  • start. Integer Number. The index of the first element to remove. If start is negative, we count backwards from the end of the array (-1 is the last element)
  • length. Integer Number. The number of elements to remove

Returns

  • A new Array containing the elements to remove. These elements are removed from this array
  • If length < 0 returns Nothing. This array is unaltered
  • If start > array.length returns Nothing. This array is unaltered

Example

var a = ["a", "b", "c"]
var b = a.slice!(0, 0)
print(a) # => ["a", "b", "c"]
print(b) # => []

var c = ["a", "b", "c"]
var d = c.slice!(0, 1)
print(c) # => ["b", "c"]
print(d) # => ["a"]

var e = ["a", "b", "c"]
var f = e.slice!(0, 5)
print(e) # => []
print(f) # => ["a", "b", "c"]

var g = ["a", "b", "c"]
var h = g.slice!(-1, 1)
print(g) # => ["a", "b"]
print(h) # => ["c"]

var i = ["a", "b", "c"]
var j = i.slice!(-1, -1)
print(i) # => ["a", "b", "c"]
print(j) # => Nothing