Bubblegum::Object::Array - Common Methods for Operating on Array References
version 0.11
use Bubblegum; my $array = [1..5]; say $array->keyed('a'..'d'); # {a=>1,b=>2,c=>3,d=>4}
Array methods work on array references. Users of these methods should be aware of the methods that modify the array reference itself as opposed to returning a new array reference. Unless stated, it may be safe to assume that the following methods copy, modify and return new array references based on their subjects. It is not necessary to use this module as it is loaded automatically by the Bubblegum class.
my $array = [2..5]; $array->all > 1; # 1; true $array->all > 3; # 0; false
The all method returns true if all of the elements in the subject meet the criteria set by the operand and rvalue.
my $array = [2..5]; $array->any > 5; # 0; false $array->any > 3; # 1; true
The any method returns true if any of the elements in the subject meet the criteria set by the operand and rvalue.
my $array = [1..5]; $array->count; # 5
The count method returns the number of elements within the subject.
my $array = [1,2,undef,4,5]; $array->defined(2); # 0; false $array->defined(1); # 1; true
The defined method returns true if the element within the subject at the index specified by the argument meets the criteria for being defined, otherwise it returns false.
my $array = [1..5]; $array->delete(2); # 3
The delete method returns the value of the element within the subject at the index specified by the argument after removing it from the array.
my $array = ['a'..'g']; $array->each(sub{ my $index = shift; # 0 my $value = shift; # a ... });
The each method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the index and value at the current position in the loop.
my $array = ['a'..'g']; $array->each_key(sub{ my $index = shift; # 0 ... });
The each_key method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the index at the current position in the loop.
my $array = ['a'..'g']; $array->each_n_values(4, sub{ my $value_1 = shift; # a my $value_2 = shift; # b my $value_3 = shift; # c my $value_4 = shift; # d ... });
The each_n_values method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the next n values until all values have been seen.
my $array = ['a'..'g']; $array->each_value(sub{ my $value = shift; # a ... });
The each_value method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop.
my $array = ['a'..'g']; $array->empty; # []
The empty method drops all elements from the subject. Note, this method modifies the subject.
my $array = [1,2,3,4,5]; $array->exists(5); # 0; false $array->exists(0); # 1; true
The exists method returns true if the element within the subject at the index specified by the argument exists, otherwise it returns false.
my $array = [1..5]; $array->first; # 1
The first method returns the value of the first element in the subject.
my $array = [1..5]; $array->get(0); # 1;
The get method returns the value of the element in the subject at the index specified by the argument.
my $array = [1..5]; $array->grep(sub{ shift >= 3 }); # [3,4,5]
The grep method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new array reference containing the elements for which the argument evaluated true.
my $array = [1..5]; $array->head; # 1
The head method returns the value of the first element in the subject.
my $array = [1..5]; my $iterator = $array->iterator; while (my $value = $iterator->next) { say $value; # 1 }
The iterator method returns a code reference which can be used to iterate over the subject. Each time the iterator is executed it will return the next element in the subject until all elements have been seen, at which point the iterator will return an undefined value.
my $array = [1..5]; $array->join; # 12345 $array->join(', '); # 1, 2, 3, 4, 5
The join method returns a string consisting of all the elements in the subject joined by the join-string specified by the argument. Note, if the argument is omitted, an empty string will be used as the join-string.
my $array = [1..5]; $array->keyed('a'..'d'); # {a=>1,b=>2,c=>3,d=>4}
The keyed method returns a hash reference where the arguments become the keys, and the elements of the subject become the values.
my $array = ['a'..'d']; $array->keys; # [0,1,2,3]
The keys method returns an array reference consisting of the indicies of the subject.
my $array = [1..5]; $array->last; # 5
The last method returns the value of the last element in the subject.
my $array = [1..5]; $array->length; # 5
The length method returns the number of elements in the subject.
my $array = [1..5]; $array->list; # (1,2,3,4,5)
The list method returns the elements in the subject as a list.
my $array = [1..5]; $array->map(sub{ shift + 1 }); # [2,3,4,5,6]
The map method iterates over each element in the subject, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new array reference containing the elements for which the argument returns a value or non-empty list.
my $array = [8,9,1,2,3,4,5]; $array->max; # 9
The max method returns the element in the subject with the highest numerical value. All non-numerical element are skipped during the evaluation process.
my $array = [8,9,1,2,3,4,5]; $array->min; # 1
The min method returns the element in the subject with the lowest numerical value. All non-numerical element are skipped during the evaluation process.
my $array = [2..5]; $array->none <= 1; # 1; true $array->none <= 2; # 0; false
The none method returns true if none of the elements in the subject meet the criteria set by the operand and rvalue.
my $array = [5,4,3,2,1]; $array->nsort; # [1,2,3,4,5]
The nsort method returns an array reference containing the values in the subject sorted numerically.
my $array = [2..5]; $array->one == 5; # 1; true $array->one == 6; # 0; false
The one method returns true if only one of the elements in the subject meet the criteria set by the operand and rvalue.
my $array = [1..5]; $array->pairs; # [[0,1],[1,2],[2,3],[3,4],[4,5]]
The pairs method is an alias to the pairs_array method.
my $array = [1..5]; $array->pairs_array; # [[0,1],[1,2],[2,3],[3,4],[4,5]]
The pairs_array method returns an array reference consisting of array references where each sub array reference has two elements corresponding to the index and value of each element in the subject.
my $array = [1..5]; $array->pairs_hash; # {0=>1,1=>2,2=>3,3=>4,4=>5}
The pairs_hash method returns a hash reference where each key and value pairs corresponds to the index and value of each element in the subject.
my $array = [1..5]; $array->pop; # 5
The pop method returns the last element of the subject shortening it by one. Note, this method modifies the subject.
my $array = [1..5]; $array->push(6,7,8); # [1,2,3,4,5,6,7,8]
The push method appends the subject by pushing the agruments onto it and returns itself. Note, this method modifies the subject.
my $array = [1..5]; $array->random; # 4
The random method returns a random element from the subject.
my $array = [1..5]; $array->reverse; # [5,4,3,2,1]
The reverse method returns an array reference containing the elements in the subject in reverse order.
my $array = [1..5]; $array->rotate; # [2,3,4,5,1] $array->rotate; # [3,4,5,1,2] $array->rotate; # [4,5,1,2,3]
The rotate method rotates the elements in the subject such that first elements becomes the last element and the second element becomes the first element each time this method is called. Note, this method modifies the subject.
my $array = [5,4,3,2,1]; $array->rnsort; # [5,4,3,2,1]
The rnsort method returns an array reference containing the values in the subject sorted numerically in reverse.
my $array = ['a'..'d']; $array->rsort; # ['d','c','b','a']
The rsort method returns an array reference containing the values in the subject sorted alphanumerically in reverse.
my $array = [1..5]; $array->set(4,6); # [1,2,3,4,6]
The set method returns the value of the element in the subject at the index specified by the argument after updating it to the value of the second argument.
my $array = [1..5]; $array->shift; # 1
The shift method returns the first element of the subject shortening it by one. Note, this method modifies the subject.
my $array = [1..5]; $array->size; # 5
The size method is an alias to the length method.
my $array = [1..5]; $array->slice(2,4); # [3,5]
The slice method returns an array reference containing the elements in the subject at the index(es) specified in the arguments.
my $array = ['d','c','b','a']; $array->sort; # ['a','b','c','d']
The sort method returns an array reference containing the values in the subject sorted alphanumerically.
my $array = [1..5]; $array->sum; # 15
The sum method returns the sum of all values for all numerical elements in the subject. All non-numerical element are skipped during the evaluation process.
my $array = [1..5]; $array->tail; # [2,3,4,5]
The tail method returns an array reference containing the second through the last elements in the subject omitting the first.
my $array = [1,1,1,1,2,3,1]; $array->unique; # [1,2,3]
The unique method returns an array reference consisting of the unique elements in the subject.
my $array = [1..5]; $array->unshift(-2,-1,0); # [-2,-1,0,1,2,3,4,5]
The unshift method prepends the subject by pushing the agruments onto it and returns itself. Note, this method modifies the subject.
my $array = [1..5]; $array->values; # [1,2,3,4,5]
The values method returns an array reference consisting of the elements in the subject. This method essentially copies the content of the subject into a new container.
Al Newkirk <anewkirk@ana.io>
This software is copyright (c) 2013 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Bubblegum, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Bubblegum
CPAN shell
perl -MCPAN -e shell install Bubblegum
For more information on module installation, please visit the detailed CPAN module installation guide.