The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Sidef::Types::Array::Array

DESCRIPTION

This module provides the support for arrays.

SYNOPSIS

  • Creating an array

        var array = (Array.new(obj1, obj2, ...));

    or

        var array = [obj1, obj2, ...];

    or

        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

Inherits methods from:

    * Sidef
    * Sidef::Convert::Convert

METHODS

!=

Obj != Obj -> Obj

Return the

Aliases: ne

and

Alias for &.

multiply

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.

    [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

Return the

-

Obj - Obj -> Obj

Return the

Aliases: subtract

--

Array-- -> Array

Remove the last element from the array.

    [1, 2, 3]--;        # returns [1, 2]

WARNING! This method modifies the array in place.

Returns the self object.

...

Obj ... Obj -> Obj

Return the

Aliases: to_list

/

Obj / Obj -> Obj

Return the

Aliases: div, divide

:

Obj : Obj -> Obj

Return the

Aliases: pair_with, pairWith

<<

Obj << Obj -> Obj

Return the

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']

WARNING! This method modifies the array in place.

Returns the self object.

==

Obj == Obj -> Obj

Return the

Aliases: eq, is, equals

pop

  • 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']

WARNING! This method modifies the array in place.

^

Obj ^ Obj -> Obj

Return the

Aliases: xor

abbrev

Array.abbrev() -> Obj

Return the

Aliases: abbreviations

all

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

any

Array.any() -> Obj

Return the

bshuffle

Array.bshuffle() -> Obj

Return the

Aliases: best_shuffle, bestShuffle

combination

Array.combination() -> Obj

Return the

Aliases: combinations

contains

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

Returns an object of type: Sidef::Types::Bool::Bool

contains_all

Array.contains_all() -> Obj

Return the

Aliases: containsAll

contains_any

Array.contains_any() -> Obj

Return the

Aliases: containsAny

contains_type

Array.contains_type() -> Obj

Return the

Aliases: containsType

copy

Array.copy() -> Array

Returns a copy of the self array.

count

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

defined

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

Returns an object of type: Sidef::Types::Bool::Bool

delete

Array.delete() -> Obj

Return the

Aliases: remove

delete_first

Array.delete_first() -> Obj

Return the

Aliases: deleteFirst, remove_first, removeFirst

delete_first_if

Array.delete_first_if() -> Obj

Return the

Aliases: deleteFirstIf, remove_first_if, removeFirstIf

delete_if

Array.delete_if() -> Obj

Return the

Aliases: deleteIf, remove_if, removeIf

dump

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

each_index

Array.each_index() -> Obj

Return the

each_with_index

Array.each_with_index() -> Obj

Return the

offset

Array.offset() -> Number

Returns the last index of the array.

    ['a','b','c'].offset;       # returns: 2

Returns an object of type: Sidef::Types::Number::Number

exists

Array.exists() -> Obj

Return the

Aliases: existsIndex

find

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.

first

Array.first() -> Obj

Returns the first element of the array.

firstIndex

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.

    ['a','b','c'].firstIndex { _ == 'c' };      # returns: 3
    [1,2,3,4,5,6].firstIndex { _ >= 100 };      # returns: -1

Returns an object of type: Sidef::Types::Number::Number

flatten

Array.flatten() -> Obj

Return the

for

Array.for() -> Obj

Return the

Aliases: each, foreach

ft

Array.ft() -> Obj

Return the

Aliases: from_to, fromTo

get

Array.get() -> Obj

Return the

Aliases: item

filter

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]

Returns a new object of the same type as the self object.

group_by

Array.group_by() -> Obj

Return the

Aliases: groupBy

reduce

  • 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.

        [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)

Returns an object of the same type as the elements of the array.

insert

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']

WARNING! This method modifies the array in place.

Returns the self object.

is_empty

Array.is_empty() -> Obj

Return the

Aliases: isEmpty

join

Array.join(String) -> String

Join the array as a string.

    qw(apples pears apricots).join(", ");      # returns: "apples, pears, apricots"

Returns an object of type: Sidef::Types::String::String

joinInsert

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']

Returns a new object of the same type as the self object.

last

Array.last() -> Obj

Returns the last element of the array.

lastIndex

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.

    [1,2,3,4,5,6].firstIndex { _**2 < 20 };     # returns: 3

Returns an object of type: Sidef::Types::Number::Number

last_uniq

Array.last_uniq() -> Obj

Return the

Aliases: lastUniq, last_unique, lastUnique

len

Array.len() -> Obj

Return the

Aliases: size, length

lev

Array.lev() -> Obj

Return the

Aliases: leven, levenshtein

make

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']

Returns a new object of the same type as the self object.

map

Array.map() -> Obj

Return the

Aliases: collect

max

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'

Returns an object of the same type as the elements of the array.

max_by

Array.max_by() -> Obj

Return the

Aliases: maxBy

min

Array.min() -> Obj

Works just like the max() method, except that it returns the lower element, instead of the greatest one.

    [4,2,3,5,7,8].min;       # returns: 2
    ['s','p','d'].min;       # returns: 'd'

Returns an object of the same type as the elements of the array.

min_by

Array.min_by() -> Obj

Return the

Aliases: minBy

minmax

Array.minmax() -> Obj

Return the

new

Array.new(Obj1, Obj2, ...) -> Array

Creates a new array.

    Array.new(1,2,3);       # returns: [1, 2, 3]

Returns a new object of the same type as the self object.

pack

Array.pack() -> Obj

Return the

pairs

Array.pairs() -> Obj

Return the

permute

Array.permute() -> Obj

Return the

Aliases: permutations

pick

Array.pick() -> Obj

Return the

Aliases: rand, sample

pop_at

Array.pop_at() -> Obj

Return the

Aliases: popAt, delete_index, deleteIndex

pop_rand

Array.pop_rand() -> Obj

Return the

Aliases: popRand

prepend

Array.prepend() -> Obj

Return the

Aliases: unshift

prod

Array.prod() -> Obj

Return the

Aliases: product

range

Array.range() -> Array

Returns a new array with numbers ranging from 0 to array.offset.

    ['x','y','z'].range;       # returns: [0, 1 ,2]

This method always returns an object of type: Sidef::Types::Array::Array

reduce_operator

Array.reduce_operator() -> Obj

Return the

reduce_pairs

Array.reduce_pairs() -> Obj

Return the

Aliases: reducePairs

resize

Array.resize() -> Obj

Return the

Aliases: resize_to, resizeTo

reverse

Array.reverse() -> Obj

Return the

Aliases: reversed

rotate

Array.rotate() -> Obj

Return the

shuffle

Array.shuffle() -> Obj

Return the

slice

Array.slice() -> Obj

Return the

sort

Array.sort() -> Obj

Return the

splice

Array.splice() -> Obj

Return the

combine

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)

Returns an object of the same type as the elements of the array.

swap

Array.swap() -> Obj

Return the

take_left

Array.take_left() -> Obj

Return the

Aliases: takeLeft

take_right

Array.take_right() -> Obj

Return the

Aliases: takeRight

to_h

Array.to_h() -> Obj

Return the

Aliases: to_hash, toHash

to_s

Array.to_s() -> Obj

Return the

uniq

Array.uniq() -> Obj

Return the

Aliases: unique, distinct

unroll_operator

Array.unroll_operator() -> Obj

Return the

mesh

Alias for &&.

|

Obj | Obj -> Obj

Return the

Aliases: or

push

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().

WARNING! This method modifies the array in place.

Returns the self object.

»

Obj » Obj -> Obj

Return the

Aliases: assign_to, assignTo, unroll_to, unrollTo