I much prefer the far simpler syntax of nFor.

And nothing's stopping you from providing that interface.

#! perl -slw use strict; use Algorithm::Loops qw( NestedLoops ); sub nFor(&@) { my $cb = shift; NestedLoops([ map [ 0..$_-1 ], @_ ], $cb); } my @digits = 1 .. 3; nFor { print join '', @digits[ @_ ]; } ( 3 ) x 4;

than the tortuous documentation for NestedLoops which I've still never wrapped by brain around.

Say you want

apple, dog, 1 apple, dog, 2 apple, dog, III apple, cat, 1 apple, cat, 2 apple, cat, III apple, platypus, 1 apple, platypus, 2 apple, platypus, III orange, dog, 1 orange, dog, 2 orange, dog, III orange, cat, 1 orange, cat, 2 orange, cat, III orange, platypus, 1 orange, platypus, 2 orange, platypus, III tomato, dog, 1 tomato, dog, 2 tomato, dog, III tomato, cat, 1 tomato, cat, 2 tomato, cat, III tomato, platypus, 1 tomato, platypus, 2 tomato, platypus, III
from
# Variable number of lists of variable length my @lists = ( [qw( apple orange tomato )], [qw( dog cat platypus )], [qw( 1 2 III )], );

That's what NestedLoops is for:

local $, = ", "; local $\ = "\n"; NestedLoops(\@lists, sub { print @_ });

nFor would require:

local $, = ", "; local $\ = "\n"; nFor { print map $lists[$_][ $_[$_] ], 0..$#_; } map 0+@$_, @lists;

Maybe you wouldn't have a problem understanding NestedLoops if it provided a map-ish interface?

sub nested(&@) { my $cb = shift; NestedLoops(\@_, $cb); } local $, = ", "; local $\ = "\n"; nested { print @_; } @lists;

Update: Switched initial line to something less rude.


In reply to Re^4: Generating lists of strings by ikegami
in thread Generating lists of strings by Anonymous Monk

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.