"Sputnik" help  
Sputnik Help
redo;
~redo; redoIf(<cond>);
~redoIf(<cond>); redoUnless(<cond>);
~redoUnless(<cond>);

Description

This statement causes most operations such as loops to start over at the current iteration.

Parameters

~
If a redo is preceeded by a ~ symbol it will trigger a redo all this can used to fully restart the current block of code.

Remarks

The redo statement in Sputnik is a powerful tool that allows you to restart the current iteration of loops and certain operations. It provides a convenient way to repeat an iteration from the beginning, effectively resetting the loop's state.

When encountering the redo statement, Sputnik will re-evaluate the loop conditions and re-execute the loop body, allowing you to perform additional processing or re-evaluate certain conditions before continuing to the next iteration. This feature adds flexibility and control to your looping logic, enabling you to fine-tune the behavior of your loops based on runtime conditions.

In addition, Sputnik introduces the unique ~redo syntax, where the tilde symbol (~) precedes the redo statement. This special syntax triggers a complete restart of the current block of code, effectively restarting the loop or operation from the beginning. This allows you to start fresh and reprocess the entire block, making it a powerful tool for handling complex scenarios that require full code restarts.

Whether you need to repeat a specific iteration within a loop or restart the entire block of code, Sputnik's redo statement and ~redo syntax provide a convenient and efficient way to achieve these functionalities. Leverage these features to enhance the control and flow of your code within loops and other operations in Sputnik.

Related

Loop Statements
Try Statement
Switch Statement
Select Statement
Continue Statement
Break Statement

Example

Using it on a While statement:

my $value = 0;
while (true)
{
    say "Value is: $value";
    $value++;
    if ($value < 5)
    {
        redo;
    }
    break;
}
// PRINTS
// Value is: 0
// Value is: 1
// Value is: 2
// Value is: 3
// Value is: 4

Using it on a Try statement:

my $value = 0;
try
{
    $value++;
    throw new Exception("Error!");
}
finally
{
    say "finally?";
    if ($value < 5)
    {
        redo;
    }
}
// PRINTS
// finally?
// finally?
// finally?
// finally?
// finally?

Using it with a Foreach statement:

my $value = 0;
foreach (my $animal in array("cat", "dog", "fox"))
{
    say "Animal is: $animal";
    if ($animal == "dog")
    {
        $value++;
        if ($value < 3)
        {
            redo;
        }
    }
}
// PRINTS
// Animal is: cat
// Animal is: dog
// Animal is: dog
// Animal is: dog
// Animal is: fox

Here is an example of using it to redo a case on a Switch statement:

my $a = "Cat"; 
my $i = 0;
Switch( $a )
{
    case "Cat":
        say "Cat ($i)";
        $i++;
        if ($i < 3)
            redo;
}
// PRINTS
// Cat (0)
// Cat (1)
// Cat (2)

That is basically all it does cause the current iteration to repeat.

While not using the redo statement you can however on a Switch or Select statement use the special GotoStart to redo the entire thing such as:

my $a = "Cat"; 
Switch( $a )
{
    case "FoX":
        say "FoX";
        break;
    case "Cat":
        say "Cat";
        $a = "FoX";
        GotoStart;
}
// PRINTS
// Cat
// Fox

This causes the entire thing to redo again.

You can use If/Unless with a redo like so:

my $value = 0;
while (true)
{
    say "Value is: $value";
    $value++;
    redoIf($value < 3);
    break;
}
// PRINTS
// Value is: 0
// Value is: 1
// Value is: 2

my $value = 0;
while (true)
{
    say "Value is: $value";
    $value++;
    redoUnless($value >= 3);
    break;
}
// PRINTS
// Value is: 0
// Value is: 1
// Value is: 2

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org