Far better solutions have been presented, but here is another. I was considering how to do if we had tail recursion. I had been hoping to place all code in the argument list...
sub parts {
my ( $cnt, $ra_out, $ra_val, $ra_in ) = @_;
if( $#$ra_val != $cnt ){ #working array not full
if( $#$ra_in > -1 ){ #fill it up
push(@$ra_val, shift(@$ra_in));
parts( $cnt, $ra_out, $ra_val, $ra_in );
} else { #done with list
return;
}
} else {
push(@$ra_out, join('',@$ra_val)); #add to outp
+ut array, joined ltr string
shift(@$ra_val); #rip off fir
+st ltr
push(@$ra_val, shift(@$ra_in)) if($#$ra_in > -1); #add new ltr
+, if any remaining
parts( $cnt, $ra_out, $ra_val, $ra_in );
}
}
my @in = split( '', "computer" ); # input array of individual letters
my @out; # out array of strings each of $cnt
++1 ltrs
my @val; # working array of individual lette
+rs, $cnt+1 elements
parts( 2, \@out, \@val, \@in );
#results in @out
..Otto
-
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.