Your sub is confusing. Note that the return statement is never executed. (You can replace it with die.) The code requires you to know exactly what happens when @set becomes empty. Are you sure you know the values of the special variable $#set, the range operator 0..$#set, and how foreach handles that? The code below avoids these issues, but is equivalent. At first, my logic appears more complicated, but I think you will find it easier to follow. Add the same debugging if you wish. Once you see what is going on, you should be able to see how the terse original is really the same thing.
use strict; use warnings; sub permutation { my ($perm,@set) = @_; unless(@set) { print "$perm\n" || die("Print error:$!"); } else { permutation($perm.$set[$_], (@set[0..$_-1],@set[$_+1..$#set]) ) foreach (0..$#set); } return; } my @input = (qw/a b c d/); permutation('',@input);

UPDATE: I now believe that you original code had a serious error and it was only by pure luck that it worked at all. The '||' operator before the 'return' should be an 'and' operator. With this change, function appears to work exactly the same. The difference is that now, the 'return' is executed immediately after every print. The behavior of 'foreach' in this special case is no longer an issue. Note that the lower priority operator 'and' instead of '&&' is needed to make the string bind to the print rather than the '&&'. The reverse logic is needed because print returns a true value on success. I have never seen a failure.

Bill

In reply to Re: Please help me understand this permutation sub? by BillKSmith
in thread Please help me understand this permutation sub? by mdunnbass

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.