Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found