Swap two variables

From CodeCodex

Implementation[edit]

C++[edit]

#include <algorithm>

std::swap(a, b);

Go[edit]

a, b = b, a

OCaml[edit]

OCaml variables are immutable, so we can only create a new local binding

let a, b = b, a in
  (* do stuff here *)

Perl[edit]

($a, $b) = ($b, $a);


PHP[edit]

// Test variables
$a = "content a";
$b = "content b";
 
// Swap $a and $b
list($a, $b) = array($b, $a);
 
// Debug print
print "variable a: $a\n";
print "variable b: $b\n";
 
/*
Output:
 
variable a: content b
variable b: content a
*/

Python[edit]

a, b = b, a

Ruby[edit]

a, b = b, a

WinBatch[edit]

; Swap two variables.

; Example 1. Dim-1 array.

arrSwapEx1 = ArrDimension (4)
arrSwapEx1 [0] = 1
arrSwapEx1 [1] = 2  ; <==
arrSwapEx1 [2] = 3
arrSwapEx1 [3] = 4  ; <==
ArraySwapElements (arrSwapEx1, 1, 0, 0, 0, 0, 3, 0, 0, 0, 0)
; Result
; arrSwapEx1 [0] = 1
; arrSwapEx1 [1] = 4  ; <==
; arrSwapEx1 [2] = 3
; arrSwapEx1 [3] = 2  ; <==


; Example 2. Dim-2 array.

arrSwapEx2 = ArrDimension (2, 3)
arrSwapEx2 [0, 0] = 10
arrSwapEx2 [0, 1] = 11  ; <==
arrSwapEx2 [0, 2] = 12
arrSwapEx2 [1, 0] = 20
arrSwapEx2 [1, 1] = 21
arrSwapEx2 [1, 2] = 22  ; <==
ArraySwapElements (arrSwapEx2, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0)
; Result
; arrSwapEx2 [0,0] = 10
; arrSwapEx2 [0,1] = 22  ; <==
; arrSwapEx2 [0,2] = 12
; arrSwapEx2 [1,0] = 20
; arrSwapEx2 [1,1] = 21
; arrSwapEx2 [1,2] = 11  ; <==


; Example 3. Dim-3 array.

arrSwapEx3 = ArrDimension (2, 3, 2)
arrSwapEx3 [0, 0, 0] = 1
arrSwapEx3 [0, 0, 1] = ""
arrSwapEx3 [0, 1, 0] = ""
arrSwapEx3 [0, 1, 1] = "Hello"  ; <==
arrSwapEx3 [0, 2, 0] = ""
arrSwapEx3 [0, 2, 1] = ""
arrSwapEx3 [1, 0, 0] = ""
arrSwapEx3 [1, 0, 1] = ""
arrSwapEx3 [1, 1, 0] = ""
arrSwapEx3 [1, 1, 1] = "World"  ; <==
arrSwapEx3 [1, 2, 0] = ""
arrSwapEx3 [1, 2, 1] = 12
ArraySwapElements (arrSwapEx3, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0) ; 1.
ArraySwapElements (arrSwapEx3, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0) ; 2.
; Result
; arrSwapEx3 [0,0,0] = 12       ; <== 1.
; arrSwapEx3 [0,0,1] = ""
; arrSwapEx3 [0,1,0] = ""
; arrSwapEx3 [0,1,1] = "World"  ; <== 2.
; arrSwapEx3 [0,2,0] = ""
; arrSwapEx3 [0,2,1] = ""
; arrSwapEx3 [1,0,0] = ""
; arrSwapEx3 [1,0,1] = ""
; arrSwapEx3 [1,1,0] = ""
; arrSwapEx3 [1,1,1] = "Hello"  ; <== 2.
; arrSwapEx3 [1,2,0] = ""
; arrSwapEx3 [1,2,1] = 1        ; <== 1.

; Syntax
; ArraySwapElements (array, subA1, subA2, subA3, subA4, subA5, subB1, subB2, subB3, subB4, subB5)
; "subA1" to "subA5" specify the subscripts of element "A".
; "subB1" to "subB5" specify the subscripts of element "B".


; Example 4. Integer variables.

; Set content.
intA = 1
intB = 2

; Swap content.
intTemp = intA
intA = intB
intB = intTemp
Drop (intTemp) ; Temporary variable can be dropped after swapping.
; Result
; intA = 2
; intB = 1


; Example 5. String variables.

; Set content.
strA = "Hello"
strB = "World"

; Swap content.
strTemp = strA
strA = strB
strB = strTemp
Drop (strTemp) ; Temporary variable can be dropped after swapping.
; Result
; strA = "World"
; strB = "Hello"

Exit
; This code example was written by Detlev Dalitz.20090706.2130.CEST