Sidef::Types::Array::Array
This module provides the support for arrays.
Creating an array
var array = (Array.new(obj1, obj2, ...));
or
var array = [obj1, obj2, ...];
var array = qw(word1 word2 ...);
Retrieving an element
var elem = array[index];
Modifying an element
array[index] = someObj;
Applying methods on arrays
array->method(argument1, argument2, ...);
Inherits methods from:
* Sidef * Sidef::Convert::Convert
Obj != Obj -> Obj
Return the
Aliases: ne
Alias for &.
&
Array.multiply() -> Obj
Works just like the combine() method, except that it multiplies each element of the array, using the * method. Useful for working with matrices.
combine()
*
[5,6,7].multiply; # returns the product number (210)
Returns an object of the same type as the elements of the array.
Array + Array -> Array
Concatenate two arrays.
[1, 2] + [3, 4]; # returns [1, 2, 3, 4]
Aliases: concat().
Returns a new object of the same type as the self object.
Array ++ Obj -> Array
Append one element to array.
array ++ "str"; # "str" has been pushed into array
Array++ -> Array
Increases the array's size by one element.
array++; # array size is increased by one
WARNING! This method modifies the array in place.
Returns the self object.
Obj += Obj -> Obj
Obj - Obj -> Obj
Aliases: subtract
Array-- -> Array
Remove the last element from the array.
[1, 2, 3]--; # returns [1, 2]
Obj ... Obj -> Obj
Aliases: to_list
Obj / Obj -> Obj
Aliases: div, divide
Obj : Obj -> Obj
Aliases: pair_with, pairWith
Obj << Obj -> Obj
Aliases: shift, drop_left, dropLeft, drop_first, dropFirst
Slice = Array -> Slice
This method is used to change the elements in a slice of array.
array = ["a", "b", "c"]; array[1, 3, 5] = ["x", "y", "z"]; # changes the slice elements with the elements from the argument array # array is now: ['a', 'x', 'c', 'y', nil, 'z']
Obj == Obj -> Obj
Aliases: eq, is, equals
Array.pop(Number) -> Obj
Remove the element from the specified position.
array = qw(a b c); array.pop(1); # returns the removed element from position 1: 'b' # array contains: ['a', 'c']
Array.pop() -> Obj
Removes the last element of the array.
array = qw(a b c); array.pop; # returns the removed last element: 'c' # array contains: ['a', 'b']
Obj ^ Obj -> Obj
Aliases: xor
Array.abbrev() -> Obj
Aliases: abbreviations
Array.all(Block) -> Bool
Returns true if all the array's elements passes the block test.
["a", "b", "c"].all { _ < "d" }; # true [0, 2, 4, 6, 8].all { _ % 2 == 0 }; # true [101, 102, 103].all { _ > 102 }; # false
Returns an object of type: Sidef::Types::Bool::Bool
Sidef::Types::Bool::Bool
Array.any() -> Obj
Array.bshuffle() -> Obj
Aliases: best_shuffle, bestShuffle
Array.combination() -> Obj
Aliases: combinations
Array.contains(Obj) -> Bool
Returns true if the array contains the argument object.
['x','y','z'].contains('y'); # true [1,[2,3],4,5].contains([2,3]); # true [1,2,[3],4,5].contains(3); # false
Array.contains_all() -> Obj
Aliases: containsAll
Array.contains_any() -> Obj
Aliases: containsAny
Array.contains_type() -> Obj
Aliases: containsType
Array.copy() -> Array
Returns a copy of the self array.
Array.count(Obj) -> Number
Count the number of objects indentical with the argument.
["x", "y", "y", "z"].count("y"); # returns: 2
Aliases: count_obj(), countObj().
Returns an object of type: Sidef::Types::Number::Number
Sidef::Types::Number::Number
Array.defined(Number) -> Bool
Returns true if the element from the Number position is defined.
[1,2,3,4,5,6].defined(0); # true ['a','b',nil].defined(2); # false
Array.delete() -> Obj
Aliases: remove
Array.delete_first() -> Obj
Aliases: deleteFirst, remove_first, removeFirst
Array.delete_first_if() -> Obj
Aliases: deleteFirstIf, remove_first_if, removeFirstIf
Array.delete_if() -> Obj
Aliases: deleteIf, remove_if, removeIf
Array.dump() -> String
Returns a string representation of the array.
qw(a b c).dump; # returns: "['a', 'b', 'c']"
Returns an object of type: Sidef::Types::String::String
Sidef::Types::String::String
Array.each_index() -> Obj
Array.each_with_index() -> Obj
Array.offset() -> Number
Returns the last index of the array.
['a','b','c'].offset; # returns: 2
Array.exists() -> Obj
Aliases: existsIndex
Array.find(Block) -> Obj
Find an element inside the array.
[1, 2, 1, 7, 5, 8].find { _ >= 4 }; # returns: 7 ['Hello', 'World'].find { _ =~ /^w/i ?? }; # returns: 'World'
Returns the first element found.
Array.first() -> Obj
Returns the first element of the array.
Array.firstIndex(Block) -> Number
Returns the index of the first element which passes the block test. Works just like the find() method, except that it returns the possition of the element, not the element itself.
find()
['a','b','c'].firstIndex { _ == 'c' }; # returns: 3 [1,2,3,4,5,6].firstIndex { _ >= 100 }; # returns: -1
Array.flatten() -> Obj
Array.for() -> Obj
Aliases: each, foreach
Array.ft() -> Obj
Aliases: from_to, fromTo
Array.get() -> Obj
Aliases: item
Array.filter(Block) -> Array
Filter the array's elements via a block test.
['a','b','c'].filter { _ > 'a'}; # returns: ['b', 'c'] [1,2,3,4,5,6].filter { _ <= 3}; # returns: [1, 2, 3]
Array.group_by() -> Obj
Aliases: groupBy
Array.reduce(Block) -> Bool
Reduces array by calling Block, multiple times, setting _[0] and _[1] each time. The first call will be with _[0] and _[1] set to the first two elements of the list, subsequent calls will be done by setting _[0] to the result of the previous call and _[1] to the next element in the list.
_[0]
_[1]
[1,2,3,4].reduce { _[0] + _[1] }; # returns the sum of array's elements (1+2+3+4 = 10)
Array.reduce(String) -> Bool
Almost the same as when it takes a block argument, but much more limited.
[1,2,3,4].reduce('*'); # returns the product of array's elements (1*2*3*4 = 24)
Array.insert(Number, Obj1, Obj2, ...) -> Array
Insert elements inside the array at a given position.
array = qw(a b f); array.insert(2, 'c', 'd', 'e'); # returns: ['a', 'b', 'c', 'd', 'e', 'f']
Array.is_empty() -> Obj
Aliases: isEmpty
Array.join(String) -> String
Join the array as a string.
qw(apples pears apricots).join(", "); # returns: "apples, pears, apricots"
Array.joinInsert(Object) -> Array
Insert after each element the argument object. The array is NOT modified in place.
['a','b','c'].joinInsert('--'); # returns: ['a','--','b','--','c']
Array.last() -> Obj
Returns the last element of the array.
Array.lastIndex(Block) -> Number
Works just like firstIndex(), except that it searches backwards and returns the index of the last element of the array which passed the block test.
firstIndex()
[1,2,3,4,5,6].firstIndex { _**2 < 20 }; # returns: 3
Array.last_uniq() -> Obj
Aliases: lastUniq, last_unique, lastUnique
Array.len() -> Obj
Aliases: size, length
Array.lev() -> Obj
Aliases: leven, levenshtein
Array.make(Number, Obj) -> Array
Make a new array of Number size, contaiting the Obj on every position.
Array.make(3, Num); # returns: [0,0,0] Array.make(3, "x"); # returns: ['x','x','x']
Array.map() -> Obj
Aliases: collect
Array.max() -> Obj
Returns the greatest element of the list. Each element must have the > method associated.
>
[5,7,8,2,3,1].max; # returns: 8 ['l','w','p'].max; # returns: 'w'
Array.max_by() -> Obj
Aliases: maxBy
Array.min() -> Obj
Works just like the max() method, except that it returns the lower element, instead of the greatest one.
max()
[4,2,3,5,7,8].min; # returns: 2 ['s','p','d'].min; # returns: 'd'
Array.min_by() -> Obj
Aliases: minBy
Array.minmax() -> Obj
Array.new(Obj1, Obj2, ...) -> Array
Creates a new array.
Array.new(1,2,3); # returns: [1, 2, 3]
Array.pack() -> Obj
Array.pairs() -> Obj
Array.permute() -> Obj
Aliases: permutations
Array.pick() -> Obj
Aliases: rand, sample
Array.pop_at() -> Obj
Aliases: popAt, delete_index, deleteIndex
Array.pop_rand() -> Obj
Aliases: popRand
Array.prepend() -> Obj
Aliases: unshift
Array.prod() -> Obj
Aliases: product
Array.range() -> Array
Returns a new array with numbers ranging from 0 to array.offset.
0
array.offset
['x','y','z'].range; # returns: [0, 1 ,2]
This method always returns an object of type: Sidef::Types::Array::Array
Array.reduce_operator() -> Obj
Array.reduce_pairs() -> Obj
Aliases: reducePairs
Array.resize() -> Obj
Aliases: resize_to, resizeTo
Array.reverse() -> Obj
Aliases: reversed
Array.rotate() -> Obj
Array.shuffle() -> Obj
Array.slice() -> Obj
Array.sort() -> Obj
Array.splice() -> Obj
Array.combine() -> Obj
Combine all the elements from the array togheter. The array's elements must have a + method associated.
+
[[1,2],[3,4]].combine; # returns: [1,2,3,4] [1,2,3,4,5,6].combine; # returns the sum of the numbers (21)
Array.swap() -> Obj
Array.take_left() -> Obj
Aliases: takeLeft
Array.take_right() -> Obj
Aliases: takeRight
Array.to_h() -> Obj
Aliases: to_hash, toHash
Array.to_s() -> Obj
Array.uniq() -> Obj
Aliases: unique, distinct
Array.unroll_operator() -> Obj
Alias for &&.
&&
Obj | Obj -> Obj
Aliases: or
Array.push(Obj1, Obj2, ...) -> Array
Append some elements to array.
array = qw(s i d); array.push("e", "f"); # pushes the element to array and returns: qw(s i d e f)
Aliases: append().
Obj » Obj -> Obj
Aliases: assign_to, assignTo, unroll_to, unrollTo
To install Sidef, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sidef
CPAN shell
perl -MCPAN -e shell install Sidef
For more information on module installation, please visit the detailed CPAN module installation guide.