Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Telephone - Nested Loops

by Moron (Curate)
on Jun 22, 2007 at 13:37 UTC ( #622785=note: print w/replies, xml ) Need Help??

in reply to Telephone - Nested Loops

NestedLoops takes a structure over which it will iterate but it needs a second parameter to tell it what to do per iteration. So you could do your thing by passing it a 7- deep nested array by reference and a code ref like sub { print "$_\n" for @_; }; But as a rule, such predictable structures should never even be built (update: I have a habit of not building ranges like 0..7 even though that's small, because I don;t want to build in memory a 0..colussus by accident!). So the thing should never even get the chance to be passed to a method; rather the algorithm should be a bit smarter.

# updated to add leading zeroes sub Arbitel { # takes number of digits as argument my $count = shift; my $limit = 10**$count; for ( my $i = 0; $i < $limit; $i++ ) { print Lzro( $count, $i ) . "\n"; } } sub Lzro my ( $count, $number ) = @_; substr( ( '0' x $count ) . $number, -$count ); }
More update: my decision chart:

"Can it be mapped to a simple virtual structure? Y: don't build it and don;t bother to use a CPAN module to generate or iterate it.

N: "Can the structure be iterated virtually without building it? N: use Algorithm::

Y: "Is there a method for it in Math::Combinatorics? Y: use that then N: use Algorithm::


^M Free your mind!

Replies are listed 'Best First'.
Re^2: Telephone - Nested Loops (no)
by tye (Sage) on Jun 22, 2007 at 16:38 UTC
    NestedLoops takes a structure over which it will iterate [...]. So you could do your thing by passing it a 7- deep nested array by reference and a code ref like sub { print "$_\n" for @_; };

    Perhaps you should (re-)read the documentation for Algorithm::Loops::NestedLoops(), as you appear to misunderstand what it does, or at least your attempt to re-explain what it does is just very badly done. To start with the easy mistake: You don't have to pass code to NestedLoops() as it is happy to just give you an iterator.

    What you give to NestedLoops() is not 7-deep. It doesn't "iterate over a structure". Your description makes it sound like NestedLoops() just traverses a data structure similar to what Data::Dumper does, that it is (to borrow bad terminology from a bad "design pattern") a "visitor pattern" implementation.

    You give NestLoops() a reference to a list of ranges. So you might call this a 2-deep data structure but that'd really miss the point. It is a list of ranges and each range can just be a reference to a list but could also be something else. It doesn't iterate over this data structure. It iterates over the space of all possible ordered combinations of items from these ranges (and does so in the order most easily described as that resulting from nesting loops that each iterate over one range).

    - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://622785]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-07-20 20:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (315 votes). Check out past polls.