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

NAME

Class::Maker::Types::Array - a sophisticated but slow array class

SYNOPSIS

  use Class::Maker::Types::Array;

        Class::Maker::Types::Array->new( array => [1..100] );

                # standard

        $a->shift;

        $a->push( qw/ 1 2 3 4 / );

        $a->pop;

        $a->unshift( qw/ 5 6 7 / );

        $a->reset;

        $a->join( ', ' );

                # extended

        $a->count;

        $a->get;

        $a->set( 1..100 );

        my $clone = $a->clone;

        $a->copy_from( $b );

        $a->pick( 4 );

        $a->warp( 2, 3, 1, 0 );  # place elements in this new order of indices

        $a->union( 100..500 );

        $a->intersection( 50..100 );

        $a->diff( 50..100 );

        $a->rand;

        $a->sort;

        $a->totext;

        $a->sum;
        
        $a->unique;

        $a->permute;

        print "same" if $a->eq( $other );

        # if array of objects
 
        $a->get_where( method1 => 'myvalue' );

        $a->get_where_sref( $sref, .. );

        $a->get_where_regexp( method1 => $regexp1, .. );
        
        $a->get_where_elementselector( Class::Maker::Types::Array::ElementSelector->new( .. ) );

        $a->stats;

        $a->scale_unit;

        $a->keys( [qw( alpha beta gamma )] );

        my ($key, $value) = $a->pair(0);

        my $href=$a->as_hash;

        my $c = $a->div( 2 );

        my $d = $a->div_by_array( qw(1 2 3) );

        my $e = $a->div_by_array( $d );

        $a->scale_unit; # scales values to < 1.0

DESCRIPTION

This an object-oriented array class, which uses a method-oriented interface.

METHODS

Mostly they have the similar syntax as the native perl functions (use "perldoc -f"). If not they are documented below, otherwise a simple example is given.

 sub at : method
 sub _preinit : method
 sub push : method
 sub pop : method
 sub shift : method
 sub unshift : method
 sub count : method
 sub reset : method
 sub get : method
 sub members : method
 sub pick : method
 sub every : method
 sub join : method
 sub sort : method
 sub warp : method
 sub _fisher_yates_shuffle
 sub rand : method
 sub _algebra
 sub union : method
 sub intersection : method
 sub diff : method
 sub _calc
 sub eq : method
 sub ne : method
 sub totext : method
 sub sum : method
 sub unique : method
 sub permute : method
 sub stats : method
 sub pair : method
 sub div : method
 sub div_by_array : method
 sub div_by_array_obj : method

at($i)

Returns the element at position $i.

count

Returns the number of elements (same as @arry in scalar context).

reset

Resets the array to an empty array.

get

Returns the backend array.

pick( [step{scalar}]:2 )

Returns every 'step' (default: 2) element.

warp( @indices )

Copies the elements in the new sequence as indicated by indices.

union

Returns the union of two arrays (Array object is returned).

intersection

Returns the intersection of the two arrays (Array object is returned).

$array_new = diff

Returns the diff of the two arrays (Array object is returned).

@objects = get_where( method_name => $value, method_name1 => $value )

Call method_name of all set array and filters the ones that match to $value.

Note: All these get_where... methods return an array of the resulting objects. Empty when nothing found.

@objects = get_where_sref( sub { }, [ sub { }, ... ] )

Filters the array and returns the ones where the sref returns true. The sref get

 $_[0] : the Class::Maker::Types::Array object

 $_[1] : the set object member

so a prototypical sref would look like

 my $sref = sub {

     my $array_obj = shift;

     my $obj = shift;

     return 1 if $obj->method_name eq ...;
 }

@objects = get_where_regexp( method_name => qr/../, [ method_name => qr/../, ... ] ) get_where_regexp( [qw(method_name arg1 arg2)] => qr/../, [ method_name => qr/../, ... ] )

Filters the array which return a value that matches the regexps. To call methods with arbitrary args, give an array reference as key where the first element is the method name.

stats(void)

Returns a hashref with following keys and values determined by the array members.

 {
  count => 5,
  max => "0.217529585558676",
  mean => "0.109738802941511",
  min => undef,
  sample_range => "0.217529585558676",
  standard_deviation => "0.103038948420036",
  sum => "0.548694014707553",
  variance => "0.0106170248915069",
 }

[Note] The module Statistics::Tests::Wilcoxon is used to generate these calculations and with any update of it, the available models may increase. Refer to its documentation and locate the stats_all() method for detailed information.

keys( @names_as_keys )

This method can be used to set array key names. Once set can be accessed as an array ref. See pair method below.

  $a->keys( qw(alpha beta gamme) );

  printf "key=%s, value=%s", $a->keys->[0], $a->at(0);

pair( $i )

Returns a ( key => value ) pair. The key may be only valid if keys was set before.

  $a->keys( qw(alpha beta gamme) );

  my ( $key, $value ) = $a->pair( 0 );
 

EXPORT

None by default.

EXAMPLE

Purpose

Because most methods return Array objects itself, the can be easily further treated with Array methods. Here a rather useless, but informative example.

Code

use Class::Maker::Types::Array;

        my $a = Class::Maker::Types::Array->new( array => [1..70] );

        my $b = Class::Maker::Types::Array->new( array => [50..100] );

        $a->intersection( $b )->pick( 4 )->join( ', ' );

AUTHOR

Murat Uenalan, muenalan@cpan.org

SEE ALSO

perl, perlfunc, perlvar