PerlArray
public final class PerlArray : PerlValue
Provides a safe wrapper for Perl array (AV
).
Performs reference counting on initialization and deinitialization.
Cheat Sheet
Array of strings
my @list = ("one", "two", "three");
let list: PerlArray = ["one", "two", "three"]
Array of mixed type data (PSGI response)
my @response = (200, ["Content-Type" => "application/json"], ["{}"]);
let response: PerlArray = [200, ["Content-Type", "application/json"], ["{}"]]
Accessing elements of the array
my @list;
$list[0] = 10
push @list, 20;
my $first = shift @list;
my $second = $list[0]
let list: PerlArray = []
list[0] = 10
list.append(20)
let first = list.removeFirst()
let second = list[0]
-
Creates an empty Perl array.
Declaration
Swift
public convenience init()
-
Creates an empty Perl array.
Declaration
Swift
public convenience init(perl: PerlInterpreter = .current)
-
Initializes Perl array with elements of collection
c
.Declaration
Swift
public convenience init<C : Collection>(_ c: C, perl: PerlInterpreter = .current) where C.Iterator.Element : PerlScalarConvertible
-
Short form of
init(dereferencing:)
.Declaration
Swift
public convenience init(_ ref: PerlScalar) throws
-
Returns the specified Perl global or package array with the given name (so it won’t work on lexical variables). If the variable does not exist then
nil
is returned.Declaration
Swift
public convenience init?(get name: String, perl: PerlInterpreter = .current)
-
Returns the specified Perl global or package array with the given name (so it won’t work on lexical variables). If the variable does not exist then it will be created.
Declaration
Swift
public convenience init(getCreating name: String, perl: PerlInterpreter = .current)
-
A textual representation of the AV, suitable for debugging.
Declaration
Swift
public override var debugDescription: String
-
Fetches the element at the specified position.
Complexity
O(1).
Declaration
Swift
public func fetch<T : PerlScalarConvertible>(_ index: Int) throws -> T?
Parameters
index
The position of the element to fetch.
Return Value
nil
if the element not exists or is undefined. -
Stores the element at the specified position.
Complexity
O(1).
Declaration
Swift
public func store<T : PerlScalarConvertible>(_ index: Int, value: T)
Parameters
index
The position of the element to fetch.
value
The value to store in the array.
-
Deletes the element at the specified position.
Declaration
Swift
public func delete<T : PerlScalarConvertible>(_ index: Int) throws -> T?
Parameters
index
The position of the element to fetch.
Return Value
Deleted element or
nil
if the element not exists or is undefined. -
Deletes the element at the specified position.
Declaration
Swift
public func delete(_ index: Int)
-
Returns true if the element at the specified position is initialized.
Declaration
Swift
public func exists(_ index: Int) -> Bool
-
Frees the all the elements of an array, leaving it empty.
Declaration
Swift
public func clear()
-
The position of the first element in a nonempty array. It is always 0 and does not respect Perl variable
$[
.If the array is empty,
startIndex
is equal toendIndex
.Declaration
Swift
public var startIndex: Int
-
The array’s
past the end
position—that is, the position one greater than the last valid subscript argument.If the array is empty,
endIndex
is equal tostartIndex
.Declaration
Swift
public var endIndex: Int
-
Accesses the element at the specified position.
If the element not exists then an undefined scalar is returned. Setting a value to the nonexistent element creates that element.
Complexity
Reading an element from an array is O(1). Writing is O(1), too.Declaration
Swift
public subscript(index: Int) -> PerlScalar
Parameters
index
The position of the element to access.
-
Creates a Perl array from a Swift array of
PerlScalar
s.Declaration
Swift
public convenience init(_ array: [Element])
-
Reserves enough space to store the specified number of elements.
If you are adding a known number of elements to an array, use this method to avoid multiple reallocations. For performance reasons, the newly allocated storage may be larger than the requested capacity.
Complexity
O(n), where n is the count of the array.
Declaration
Swift
public func reserveCapacity(_ minimumCapacity: Int)
Parameters
minimumCapacity
The requested number of elements to store.
-
Adds a new element at the end of the array.
Use this method to append a single element to the end of an array.
Because arrays increase their allocated capacity using an exponential strategy, appending a single element to an array is an O(1) operation when averaged over many calls to the
append(_:)
method. When an array has additional capacity, appending an element is O(1). When an array needs to reallocate storage before appending, appending is O(n), where n is the length of the array.Complexity
Amortized O(1) over many additions.
Declaration
Swift
public func append(_ sv: Element)
Parameters
sv
The element to append to the array.
-
Removes and returns the first element of the array.
The array can be empty. In this case undefined
PerlScalar
is returned.Complexity
O(1)
Declaration
Swift
public func removeFirst() -> Element
Return Value
The first element of the array.
-
Creates Perl array from the given array literal.
Do not call this initializer directly. It is used by the compiler when you use an array literal. Instead, create a new array by using an array literal as its value. To do this, enclose a comma-separated list of values in square brackets. For example:
let array: PerlArray = [200, "OK"]
Declaration
Swift
public convenience init (arrayLiteral elements: Element...)
Parameters
elements
A variadic list of elements of the new array.