[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Recursion, anyone?





> -----Original Message-----
> From: Filipe Brandenburger
> Hey, Eric!
> <snip>
> Two things about your code: <snip>
> Tell us if you can find the mistakes.

Well, ok.  Since you asked, here is the patched version!

#!/usr/bin/perl

$max = $ARGV[0];
$continue = 1;

@el=(a..z,0..9); # or whatever you want

### changed (0..$max) to (1..$max)
### This solves the too long word problem.
for(1..$max) {push @i, 0};

while ($continue == 1)
{
  $word = "";
  for ($letter=0; $letter<@i;$letter++)
  {
    $word .= $el[$i[$letter]];  
  }
  
  print "$word\n";
  
  for($inc=0;$inc<@i;$inc++)
  {
    if($flag==0)
    {
      $i[$inc]++;

### changed @el to @el-1, because $el[@el] is empty!
### this created the shortened words

      if ($i[$inc] > (@el-1))
      {
        $i[$inc] = 0;
      } else
      {
        $flag = 1;
      }
    }
  }
  if ($flag == 0) {$continue = 0};
  $flag = 0;
}

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe