Hi,

I have an array, of which some elements are scalars and some of which are array refs. There are a lot of fields, and i don't know definitely which column will have a scalar and which will have array refs until I parse it in.

My question is: Is there a module that will multiplex a datastructure like this out to multiple rows, one for each combination of elements in the array? For example, if I have:

( static1, (ar1, ar2), static2, (ar3, ar4, ar5) )

I want it to print:

static1, ar1, static2, ar3
static1, ar1, static2, ar4
static1, ar1, static2, ar5
static1, ar2, static2, ar3
static1, ar2, static2, ar4
static1, ar2, static2, ar5


Now, I think I can be clever and use the ideas in MJD 'Higher Order Perl' and keep an agenda/stack of partially processed lines that shifts off an element, expands the first array it comes to into N arrays, not modifying the elements behind it, and putting it back on the stack. If an element in the stack has no array refs as element, just return it.

But I don't want to reinvent the wheel, and I think this may have already been done -- I just don't know what keywords to use to search for a solution. Does anybody know if a solution to this already exists as a module, and how would I phrase the problem best in a common language that people would understand?

Thanks, Matt

UPDATE: Thank you both ikegami and Fletch! I'll likely implement ikegami's, but will play with Fletch's solution.

In reply to Multiplexing array of arrays? by mpettis

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.