Array Clone

From SwiftAPI

Jump to: navigation, search


Perform a shallow copy of an array.

Contents

JavaScript

developer.mozilla.org

var a = [9, 3, 8, 5, 8, 7];
var b = a.slice();
a[1] = 1;
b[2] = 2;
// a == [9, 1, 8, 5, 8, 7]
// b == [9, 3, 2, 5, 8, 7]

PHP

Arrays are copied by default

$a = [9, 3, 8, 5, 8, 7];
$b = $a;
$a[1] = 1;
$b[2] = 2;
// $a == [9, 1, 8, 5, 8, 7]
// $b == [9, 3, 2, 5, 8, 7]

Python

python.org

a = [9, 3, 8, 5, 8, 7]
b = list(a)
a[1] = 1
b[2] = 2
# a == [9, 1, 8, 5, 8, 7]
# b == [9, 3, 2, 5, 8, 7]

Ruby

dup

ruby-doc.org

a = [9, 3, 8, 5, 8, 7]
b = a.dup
a[1] = 1
b[2] = 2
# a == [9, 1, 8, 5, 8, 7]
# b == [9, 3, 2, 5, 8, 7]

clone

ruby-doc.org

a = [9, 3, 8, 5, 8, 7]
b = a.clone
a[1] = 1
b[2] = 2
# a == [9, 1, 8, 5, 8, 7]
# b == [9, 3, 2, 5, 8, 7]
Personal tools