in reply to Re: finding number of contiguous letters
in thread finding number of contiguous letters
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: finding number of contiguous letters
by blazar (Canon) on May 23, 2007 at 20:03 UTC | |
by otto (Beadle) on May 24, 2007 at 00:04 UTC | |
by blazar (Canon) on May 24, 2007 at 10:32 UTC | |
by otto (Beadle) on May 25, 2007 at 00:27 UTC | |
by blazar (Canon) on May 25, 2007 at 07:57 UTC |
In Section
Seekers of Perl Wisdom