Hello monks and nuns!

This is more an itch than a problem: yesterday in CB came out a little simple quiz reported by 1nickt (who obviously knows many different solutions to the quiz). The problem sounded like: write a sub that receives arrays of strings and print them interleaved. I added the constraint that print order had to be not just mixed but the first element of each array, then the second etc..

obviously there are many, simple, ways to accomplish this with or without core modules. First thing that i supposed was to spot the max length of received arrays and build a loop (and this approach works ok).

But and here the itch, after a while i started mumbling about shift to death arrays skipping the max length computing and I ended with:

# DO NOT RUN THIS!! perl -e "sub inter{map {print shift @$_} @_; &inter} inter([1],[1,2],[ +1,2,3],[1,2,3,4,5])" # OUT Out of memory! 11112223345

Which unfortunately, while printing the correct result, bring to an undesired Out of memory!

Now I understand that it goes out of memory because nothing stops the recursion, so i tried to break it but with no luck: inserting in the top of the sub exit unless $_[0][0] just prints first elements of each array and exits, &inter if $_[0][0] at the end does the same.

Some insight?

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to understand and prevent 'Out of memory!' during sub recursion by Discipulus

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.