Perl do-until Statement

Perl do-until statement is another way to iterate through a looping block, but checking if a condition is met after executing the block. I suggest to you to check-up the do-while statement first.

The difference between the two looping statements is as follows:

  • in a do-while statement the block is repeatedly executed as long as the condition is true
  • a do-until statement is repeatedly executed as long as the test condition remains false
This short free tutorial will point out some relevant aspects concerning the using of this statement in Perl scripts.

The main syntax form of the Perl do-until statement is as follows:

do BLOCK until (EXPR);

Unlike the until statement where the conditional expression is tested before executing the block and if the condition is true initially the block will be skipped entirely, a Perl do-until statement executes the block at least once. Please note that the do block is not a looping block and therefore you can’t modify the flow control within the block by using the looping controls (next, last or redo).

The following script examples show you a few ways about how to use the Perl do-until statements in your Perl script applications.

Example 1.

The next example will read a command word from the standard input and it will end the loop when you’ll input the word "stop". As you see Perl continues to iterate the loop as long as the condition that follows the block remains false (i.e. the input command is not "stop").

my $command;
do {
  # ask for the input command 
  print "Command: ";       
  # get the input from STDIN and 
  # chomp off the trailing newline
  chomp($command = );  
  # convert $command to lowercase
  # convert the first character in uppercase
  $command = ucfirst $command;
until($command eq "Stop");

Example 2.

This code snippet is shown in conjunction with the arrays and hashes. It will calculate the total amount of birds which can be found in a particular household.

# initialize a hash (associative array)
%birds = (hen => 12, ducks => 15, geese => 5);
# put the bird names in an array
@birdNames = keys(%birds);

# initialize a count scalar variable
$count = 0;
do {
  # get the first element of the array
  $name = shift(@birdNames);
  # add to count the number of the current bird
  $count += $birds{$name};
} until (!@birdNames);

# displays the total amount of birds
print "Total: $count\n";
# displays Total: 32

Please note how the shift function is used here. I remind you that the shift function removes and returns the first element of an array. After removing the first element, the array will have an element less. The loop will finish when the array will have no more elements.

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!