(Perl:)
The redo command, like last and next, is used to subvert the block structure of a loop. Specifically, it causes execution immediately to resume at the top of the loop body (it is very unlike next, in that it doesn't test the loop condition again or execute any continue block or the third argument of for(;;)). It is a goto to the top of the loop (except that it is arguably more structured, since it is named and goes to a conceptually significant location]).

The form redo LABEL does the same, but for the loop with the LABEL.

Since every block is a loop (which executes only once by default), "{redo}" is an infinite loop.

Example

Here's some code to calculate the common initial substring of a list of strings (i.e. the prefix shared by all the strings):

sub cis {
  my $first = shift;
  my $i = length $first;
  for (@_) { 
    if ((substr $_, 0, $i) eq (substr $first, 0, $i)) {
      next
    }
    else {
      $i--;
      redo
    }
  }
  substr $first, 0, $i;
}
$i holds our current (least) upper bound on the length of the initial substring. If the prefix of the next string in the list is the same, $i is large enough, and we're done with that string. Otherwise, we have to reduce $i and try again.

Note.

More efficient solutions are possible, particularly for when the first string is very long. The algorithmic Way To Do It is to utilise a suffix array (easier to program) or a suffix tree.

Log in or registerto write something here or to contact authors.