i'll start off by saying, the only reason i redid the coding was to try and understand what was going on. I took the permutation subroutine from the perl cookbook and redid it without the use of passing arrays (i'm passing simple variables now, constructing and destructing them into arrays). I can better see how the program works now:
#!/usr/bin/perl use strict; permute("1 2 3 4",""); sub permute { my $old = @_[0]; my $new = @_[1]; my @old = split(/ /,$old); my @new = split(/ /,$new); print "old = @old | new = @new\n"; #helpful in seeing what's going o +n unless (@old) { print "@new\n"; } else { my $i; foreach $i (0..$#old) { my @new1 = @new; my @old1 = @old; unshift(@new1,splice(@old1,$i,1)); my $new1 = join(' ',@new1); my $old1 = join(' ',@old1); permute($old1,$new1); } } }
Fellow monks, i require your assistance in understanding this permutation program. The commented line shows what the function is doing while making the permutations:
old = 1 2 3 4 | new = 
old = 2 3 4 | new = 1
old = 3 4 | new = 2 1
old = 4 | new = 3 2 1
old =  | new = 4 3 2 1
4 3 2 1
old = 3 | new = 4 2 1
old =  | new = 3 4 2 1
3 4 2 1
old = 2 4 | new = 3 1
old = 4 | new = 2 3 1
old =  | new = 4 2 3 1
4 2 3 1
... too long to continue
i understand everything up to the first loop, but after that i start losing how the "old" array picks up info from the "new" array (seems to be working backwards?). I'm not having a perl problem, but rather a theory problem with how perl actually makes this work. If someone could give me a short step-by-step guide to what perl does with the array, i'd be most grateful. Rather than using the subroutine blindly, i'd like to get a little bit of information on how it's able to work. All help appreciated, and thanks in advance.

In reply to permutation understanding by Parham

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.