############ VectorSpace Utility ####################### package VectorSpace; use Object; @VectorSpace::ISA = qw(Object); sub init { my ($self) = @_; $self->set(space => []); return $self; } sub add_dimension { my ($self, $dim_name, $data) = @_; my $space = $self->get('space'); my $count = ref $data ? $#$data + 1 : $data; my $dimension = { name => $dim_name, count => $count, data => $data }; push(@$space, $dimension); return $dimension; } sub initialize_iterator { my ($self) = @_; my $point = []; for (@{$self->get('space')}) { push (@$point, 0); } $self->set( position => 0 ); $self->set( point => $point ); } sub point { my ($self) = @_; my $point = $self->get('point'); my $space = $self->get('space'); my @res; for (0..$#$point) { push(@res, $space->[$_]->{data}->[$point->[$_]]); } return \@res; } sub increment_iterator { my ($self) = @_; my $point = $self->get('point'); my $pos = $self->get('position'); my $space = $self->get('space'); return $self->_next_point($pos); } sub _next_point { my ($self, $pos) = @_; my $point = $self->get('point'); my $space = $self->get('space'); if ($pos > $#$point) { ## end of iteration return; } $point->[$pos]++; if ($point->[$pos] >= $space->[$pos]->{count}) { $point->[$pos] = 0; return $self->_next_point($pos+1); } return 1; } ################## End VectorSpace Utility ############# ## Sample Usage ## use Data::Dumper; my $vec = VectorSpace->new( ); $vec->add_dimension( length => [1..3] ); $vec->add_dimension( height => [1..3] ); print Dumper $vec; $vec->initialize_iterator(); my $c; while (1) { my $data = $vec->point(); print Dumper $data; last if ! $vec->increment_iterator(); }

In reply to VectorSpace by smalhotra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.