Array-AsObject

 view release on metacpan or  search on metacpan

HISTORY  view on Meta::CPAN

Version 1.00  2009-04-14
   Originally, all functionality was in Template::Plugins::ListOps.
      Moved it here and made that module a wrapper around this one
      so the list operations can be used outside of templates.

Version 1.01  2010-02-05
   When a new object is created, it starts as an empty list instead
      of an undefined value.
   Any type of element (scalar, reference, undef) can be stored in
      the list.
   Added is_subset/not_subset methods.

Version 1.02
   Renamed the module to Array::AsObject . 1.02 is the final version
      of Set::ArrayAlt .

Array::AsObject
===============

Version 1.02
   Renamed the module Set::ArrayAlt to Array::AsObject . 1.02 is the

README  view on Meta::CPAN

        is in the order they appear in the first list.

        By default, duplicate elements are treated individually unless
        $unique is passed in.

        For example, the intersection of two lists (a a b b c) and (a a c d)
        is either (a a c) or (a c). If $unique is non-zero, the second is
        given.

    is_equal
    not_equal
           $flag = $obj->is_equal($obj2 [,$unique]);
           $flag = $obj->not_equal($obj2 [,$unique]);

        These take two lists and test to see if they are equal. If an error
        is encountered, undef is returned, but no error is stored.

        The order of the elements is ignored so (a,b) = (b,a).

        If $unique is non-zero, the count of each type of element is ignored
        so (a,a,b) = (a,b). Otherwise, the count is important so (a,a,b) !=
        (a,b).

    is_subset
    not_subset
           $flag = $obj->is_subset($obj2 [,$unique]);
           $flag = $obj->not_subset($obj2 [,$unique]);

        These return 1 if the list in $obj2 is a subset of the list in $obj
        (or is NOT a subset).

        If $unique is not passed in, every element in $obj2 must have an
        instance in $obj. So (a a b) is a subset of (a a a b c) but NOT of
        (a b c).

        If $unique is passed in, every element in $obj2 must exists in $obj
        but the count is unimportant, so (a a b) is a subset of (a b c).

lib/Array/AsObject.pm  view on Meta::CPAN

      }
      return 1;
   }

   foreach my $ele (@list1,@list2) {
      return 0  if ($obj1->count($ele) != $obj2->count($ele));
   }
   return 1;
}

sub not_equal {
   return 1 - is_equal(@_);
}

sub is_subset {
   my($obj1,$obj2,$unique) = @_;

   my $class = ref($obj1);

   if (ref($obj2) ne $class) {
      return undef;

lib/Array/AsObject.pm  view on Meta::CPAN

      }
      return 1;
   }

   foreach my $ele (@list) {
      return 0  if ($obj2->count($ele) > $obj1->count($ele));
   }
   return 1;
}

sub not_subset {
   return 1 - is_subset(@_);
}

sub symmetric_difference {
   my($obj1,$obj2,$unique) = @_;

   my $class = ref($obj1);
   my $ret   = new $class;

   if (ref($obj2) ne $class) {

lib/Array/AsObject.pod  view on Meta::CPAN


By default, duplicate elements are treated individually unless
$unique is passed in.

For example, the intersection of two lists (a a b b c) and (a a c d)
is either (a a c) or (a c). If $unique is non-zero, the second is
given.

=item is_equal

=item not_equal

   $flag = $obj->is_equal($obj2 [,$unique]);
   $flag = $obj->not_equal($obj2 [,$unique]);

These take two lists and test to see if they are equal. If an
error is encountered, undef is returned, but no error is stored.

The order of the elements is ignored so (a,b) = (b,a).

If $unique is non-zero, the count of each type of element is
ignored so (a,a,b) = (a,b). Otherwise, the count is important
so (a,a,b) != (a,b).

=item is_subset

=item not_subset

   $flag = $obj->is_subset($obj2 [,$unique]);
   $flag = $obj->not_subset($obj2 [,$unique]);

These return 1 if the list in $obj2 is a subset of the list in $obj
(or is NOT a subset).

If $unique is not passed in, every element in $obj2 must have an
instance in $obj. So (a a b) is a subset of (a a a b c) but NOT of
(a b c).

If $unique is passed in, every element in $obj2 must exists in $obj
but the count is unimportant, so (a a b) is a subset of (a b c).

t/set.t  view on Meta::CPAN

  if ($op eq "union") {
     $ret = $obj1->union($obj2,@test);
  } elsif ($op eq "difference") {
     $ret = $obj1->difference($obj2,@test);
  } elsif ($op eq "intersection") {
     $ret = $obj1->intersection($obj2,@test);
  } elsif ($op eq "symmetric_difference") {
     $ret = $obj1->symmetric_difference($obj2,@test);
  } elsif ($op eq "is_equal") {
     $ret = $obj1->is_equal($obj2,@test);
  } elsif ($op eq "not_equal") {
     $ret = $obj1->not_equal($obj2,@test);
  } elsif ($op eq "is_subset") {
     $ret = $obj1->is_subset($obj2,@test);
  } elsif ($op eq "not_subset") {
     $ret = $obj1->not_subset($obj2,@test);
  }
  if (ref($ret)) {
     return $ret->list();
  }
  return $ret;
}

$obj{l1} = new Array::AsObject qw(a a b c);
$obj{l2} = new Array::AsObject qw(a c d d e);
$obj{l3} = new Array::AsObject qw(a a c d);

t/set.t  view on Meta::CPAN

symmetric_difference l4 l5 1 ~ b b

is_equal l6 l7 ~ 0

is_equal l6 l7 1 ~ 1

is_equal l7 l8 ~ 1

is_equal l7 l8 1 ~ 1

not_equal l6 l7 ~ 1

not_equal l6 l7 1 ~ 0

not_equal l7 l8 ~ 0

not_equal l7 l8 1 ~ 0

is_subset l6 l7 0 ~ 1

is_subset l6 l7 1 ~ 1

is_subset l7 l6 0 ~ 0

is_subset l7 l6 1 ~ 1

is_subset l1 l6 0 ~ 1



( run in 0.395 second using v1.01-cache-2.11-cpan-4d4bc49f3ae )