Array Pop

From SwiftAPI

(Redirected from Perl pop)
Jump to: navigation, search

Removes and returns the last element of the array.

For languages that are not shown below, you can remove the last element by using Array Remove At with the index of the last element, which is (length of array) - 1.

Contents

C++

For vectors:

#include <vector>
using namespace std;
 
int a0[] = {2, 4, 6, 8};
vector<int> a(a0, a0+4);
int x = a.back();
a.pop_back();
// a == [2, 4, 6]
// x == 8

JavaScript

Mozilla

var a = [2, 4, 6, 8];
var x = a.pop();
// a == [2, 4, 6]
// x == 8

Objective-C

developer.apple.com

- (void)removeLastObject
NSMutableArray *a = [NSMutableArray arrayWithObjects:@2, @4, @6, @8, nil];
id x = a.lastObject;
[a removeLastObject];
// a == [2, 4, 6]
// x == 8

Perl

perldoc

@a = (2, 4, 6, 8);
$x = pop(@a);
# @a == (2, 4, 6)
# $x == 8

PHP

php.net

$a = array(2, 4, 6, 8);
$x = array_pop($a);
// $a == array(2, 4, 6)
// $x == 8

Python

docs.python.org

a = [2, 4, 6, 8]
x = a.pop()
# a == [2, 4, 6]
# x == 8

Ruby

ruby-doc.org

a = [2, 4, 6, 8]
x = a.pop
# a == [2, 4, 6]
# x == 8

Swift

var a = [2, 4, 6, 8]
let x = a.last
a.removeLast()
// a == [2, 4, 6]
// x == Optional(8)
Personal tools