Perl until Statement



Perl until Menu:

1. The syntax forms
    1.1. General until statement
        1.1.1. until and an infinite loop
        1.1.2. until and the diamond operator <>
        1.1.3. until in connection with next, last, redo and continue
        1.1.4. until and arrays
        1.1.5. until and hashes (associative arrays)
    1.2. until used as a modifier


The Perl until looping statement is the opposite of the while loop, i.e. it executes repeatedly a block of code until a test condition becomes true, or in other words it executes the block of code as long as the test condition remains false. The while statement evaluates a control expression and executes the body as long as the control expression remains true.

It’s important to highlight that both looping statements – while and until – will evaluate the test condition before to execute the statement body, so if the condition is initially false (for the while statement) or true (for the until statement), the block of the statement will be skipped entirely.

The syntax forms of the two statements are almost identically, you can replace while with until and you have the syntax forms of the until statement – of course they differ in function of the test condition result (true or false). So I heartily recommend to check out my while link first and after that revisit this page.

You can also use the last, next and redo looping controls in order to change the usual behavior of the Perl until loop iteration. These three controls allow you to control the flow of execution in the until block.

In the same time you can attach a continue block to the Perl until statement if you need to execute some piece of code after the current iteration.

1. The syntax forms


Now it’s the time to have a look at the syntax forms of the Perl until statement:

    1.1. General until statement


LABEL until (EXPR) BLOCK continue BLOCK

Either the LABEL or the continue BLOCK are optional. EXPR is a conditional expression that will be evaluated before the current iteration of the loop.

You’ll find below a few ways to use the Perl until statement.

       1.1.1. until and an infinite loop


The next example will show you an infinite loop and how to end it by using the last looping control:

$count = -100;
until(0) {
  last if $count >= 100;
  $count++;
}
print "count = $count\n";
# it displays count = 100;

       1.1.2. until and the diamond operator <>


You can use the Perl until statement with the diamond operator <>, as in the following snippet code:

until ($line = ) {
    print "I am inside the main block\n";
} 
continue {
  print "I am inside the continue block\n";
}

chomp $line;
print "I typed: $line\n";

This code will read from the standard input one line of text, and because the test condition will be evaluated true, neither the body of the Perl until statement nor the continue block will be executed; the script program will continue with the statements that follows the until statement. If we assume that we run this code and type "First line" followed by the return key from the keyboard, the output of the program will be:

First line
I typed: First line

This is an example when initially the test condition is evaluated true and therefore the Perl until statement was skipped entirely. It was only a demonstrative example, if you want to use the diamond operator in connection with a loop statement, the while statement is more appropriate.

Well, is not entirely true what I said before, the next code snippet shows you another way to use until in connection with the <> diamond operator:

until($exit =~ /yes/i ) {
  print "Exit the loop? (yes/no): ";
  chomp($exit = );
}  

And a sample of output:

Do you exit the loop? (yes/no): No, I don't
Do you exit the loop? (yes/no): Yes, I do

       1.1.3. until in connection with next, last, redo and continue


The next example will show you how to use the until statement in connection with the looping controls and continue block. See the code:

$count = 10;
until($count <= 0) {
  last if $count < 3;
  next if $count%2 == 0;
  $count--, redo if $count == 7;
  print "$count ";
}
continue {
  $count--;
}
print "\n";
# it displays 9 5 3

The above code will print the odd numbers less than 10 and greater or equal to 3, excepting the number 7 (the redo control will reiterate the loop but neither executing the continue block nor testing the conditional expression, so I must to decrement the $count before to execute the redo control in order to avoid an infinite loop).

Note the using of the remainder operator and the next looping control that allowed me to skip the even numbers.

The next control, before to reiterate the loop and test the conditional expression, will execute the statements from within the continue block.

The last control will jump out of the main block without executing the continue block.

       1.1.4. until and arrays


The next example will remove one by one the elements of an array:

# initialize an array
$count = 0;
@numbers = qw(one two three four five);
until(!@numbers) {
  $count++;
  shift(@numbers); 
}
print "$count elements were deleted!\n";
# it displays: 5 elements were deleted!

I remember you that the shift function removes the first element of an array. You can use within the until block some other array functions:
  • unshift – insert an element at the beginning of an array
  • push - adds an element at the end of an array
  • pop – removes the last element of an array
but keep in mind that the Perl until loop from the above example will end only when the @numbers array will become empty, otherwise the loop will continue to iterate infinitely.

       1.1.5. until and hashes (associative arrays)


The following example is a bit similar with the previous one; after iterating 100 times the until block, it will empty the hash structure and the loop will be finished.

$count = 100;
# initialize a hash
%numbers = (1 => "one", 2 => "two",
            3 => "three", 4 => "four",
            5 => five);
until(!%numbers) {
  $count--;
  %numbers = () if $count == 0;    
}	
print "count = $count\n";
# it displays: count = 0

    1.2. until used as a modifier


STATEMENT until (EXPR)

This is a short form and uses Perl until statement as a modifier of a statement. Take a look at the next sample code:

$count = 10;
print $count, " " until --$count <=0;
print "\n";
# it displays: 9 8 7 6 5 4 3 2 1
print "count = $count\n"  # displays count = 0

Some remarks about the second line of this code. First, the $count scalar variable will be decremented and next it will be evaluated the test condition (i.e. to check up if $count is less than or equal to 0).

If the conditional expression is false, the $count variable will be printed and the loop iteration will continue; otherwise the loop will be finished and Perl will execute the third line of this code snippet.


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 do-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
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return to Perl Basics



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

Site Build It!