Array Shift

From SwiftAPI

Jump to: navigation, search

Removes and returns the first element of an array.

For languages that are not shown below, you can remove the first element by using Array Remove At with index 0.

Contents

JavaScript

developer.mozilla.org

var a = [2, 1, 2, 5, 0];
var x = a.shift();
// a == [1, 2, 5, 0]
// x == 2

Perl

@a = (2, 1, 2, 5, 0);
$x = shift(@a);
# @a == (1, 2, 5, 0)
# $x == 2

PHP

$a = array(2, 1, 2, 5, 0);
$x = array_shift($a);
// $a == array(1, 2, 5, 0)
// $x == 2

Python

a = [2, 1, 2, 5, 0]
x = a.pop(0)
# a == [1, 2, 5, 0]
# x == 2

Ruby

a = [2, 1, 2, 5, 0]
x = a.shift
# a == [1, 2, 5, 0]
# x == 2
Personal tools