Notes: Perl Lab 05

Outline

  1. for loops
  2. do while|until loops
  3. Loop controls (next, last, redo)
  4. Addition
  5. Subtraction
  6. Multiplication
  7. Division
  8. Exponents
  9. Other Math Functions

  1. For Loops

    The for loop has three expressions separated by semi-colons. The first is the initialization statement. The second is the condition which will be evaluated to either continue or terminate the loop. The third is the reinitialization expression.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 00
    # Description: An example of a for loop
    #==========================================================================
    
    for ($i = 1; $i < 10 ; $i++) {
        print "\$i is $i\n";
    }
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] 
    [mark@iguana perl] ./lab05_0.pl 
    $i is 1
    $i is 2
    $i is 3
    $i is 4
    $i is 5
    $i is 6
    $i is 7
    $i is 8
    $i is 9
    [mark@iguana perl] 
    

    In the case above, the loop is initialized with the variable $i set to a value of 1. The loop will continue until $i takes on a value greater than 10. For each iteration of the loop $i will be increased by a value of 1.

  2. Do-while|until Loops

    do while loops are identical to the while loops we used in the previous lesson with the exception that the loop is run once with no regard to the value of the while conditional. Any subsequent iterations are dependent on the evaluation of the condition.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 01
    # Description: An example of a do-while loop
    #==========================================================================
    @somevar = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
    
    $counter = 1;
    
    do {
       print "$counter value is $somevar[$counter]\n";
       $counter++;
    } while ($counter <= 6);
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] 
    [mark@iguana perl] ./lab05_1.pl 
    1 value is beta
    2 value is delta
    3 value is epsilon
    4 value is zeta
    5 value is eta
    6 value is iota
    [mark@iguana perl] 
    

    do-until loops have the same behavior as do-while loops only the loop will repeat only if the conditional evaluates as false. The example below performs the same task as the do-while example above.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 02
    # Description: An example of a do-until loop
    #==========================================================================
    @somevar = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
    
    $counter = 1;
    
    do {
       print "$counter value is $somevar[$counter]\n";
       $counter++;
    } until ($counter > 6);
    
    exit;
    

    When executed, the script above produces the same output as the do-while loop:
     
    [mark@iguana perl] 
    [mark@iguana perl] ./lab05_2.pl 
    1 value is beta
    2 value is delta
    3 value is epsilon
    4 value is zeta
    5 value is eta
    6 value is iota
    [mark@iguana perl] 
    

  3. Loop Control

    Sometimes you will want to interrupt the flow of a loop based on some expression other than the condition used to define the flow of the loop. There are three functions that can be used to alter the loop: next, last and redo.

    next will stop the processing of any statements remaining in the block and allow the loop to continue as though the entire block has been executed.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 03
    # Description: An example of a loop control with next
    #==========================================================================
    
    for ($moo = 7; $moo >= 0; $moo--) {
        next if( $moo == 4 );
        print "The number is $moo\n";
    }
    
    exit;
    

    When executed, the script above produces the following output. Notice that there is no output for the value of 4.
     
    [mark@iguana perl] ./lab05_3.pl 
    The number is 7
    The number is 6
    The number is 5
    The number is 3
    The number is 2
    The number is 1
    The number is 0
    [mark@iguana perl] 
    

    last stops the loop and any remaining statements in the block are not executed.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 04
    # Description: An example of a loop control with last
    #==========================================================================
    
    for ($moo = 7; $moo >= 0; $moo--) {
        last if( $moo == 4 );
        print "The number is $moo\n";
    }
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_4.pl 
    The number is 7
    The number is 6
    The number is 5
    [mark@iguana perl]
    

    redo causes the loop to restart but does not reinitialize the loop.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 05
    # Description: An example of a loop control with redo
    #==========================================================================
    
    for ($moo = 7; $moo >= 0; $moo--) {
        $output .= $moo;
        print "The number is $output\n";
        redo if( $output == 7654 );
    }
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_5.pl 
    The number is 7
    The number is 76
    The number is 765
    The number is 7654
    The number is 76544
    The number is 765443
    The number is 7654432
    The number is 76544321
    The number is 765443210
    [mark@iguana perl]
    

  4. Addition

    The addition operator in perl is the + character. Some examples:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 06
    # Description: An example of addition in perl
    #==========================================================================
    
    # set values of the variables
    $x = 4;
    $y = 3;
    
    # normal addition
    $var = $x + $y;
    print "Value of \$var is $var\n";
    
    # addition during assignment.
    $x += $y;  # Same as $x = $x + $y.
    print "Value of \$x is $x\n";
    
    # autoincrement $x by one 
    $x++;
    print "Value of \$x is now $x\n";
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_6.pl 
    Value of $var is 7
    Value of $x is 7
    Value of $x is now 8
    [mark@iguana perl]
    

  5. Subtraction

    The subtraction operator in perl is the - character. Some examples:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 07
    # Description: An example of subtraction in perl
    #==========================================================================
    
    # set values of the variables
    $x = 10 - 4;
    $y = 3;
    
    # normal subtraction
    $var = $x - $y;
    print "Value of \$var is $var\n";
    
    # subtraction during assignment.
    $x = 10 - 4;
    $x -= $y; # same as $x = $x - $y.
    
    
    print "Value of \$x is $x\n";
    
    # autodecrement $x by one 
    $x--;
    print "Value of \$x is now $x\n";
    
    exit;
    
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_7.pl 
    Value of $var is 3
    Value of $x is 3
    Value of $x is now 2
    [mark@iguana perl]
    

  6. Multiplication

    The multiplication operator in perl is the * character. Some examples:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 08
    # Description: multiplication examples
    #==========================================================================
    
    # set values of the variables
    $x = 10 * 4;
    $y = 3;
    
    # normal multiplication
    $var = $x * $y;
    print "Value of \$var is $var\n";
    
    # multiplication during assignment.
    $x *= $y;
    print "Value of \$x is $x\n";
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_8.pl 
    Value of $var is 120
    Value of $x is 120
    [mark@iguana perl]
    

  7. Division

    The division operator in perl is the / character. Normal division will return the value of the operation with any fractional parts represented as decimal values. To get the remainder of a division operation the % operator may be used. Some examples:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 09
    # Description: Division examples
    #==========================================================================
    
    # set values of the variables
    $x = 100 / 4;  # 25
    $y = 3;
    
    # normal division
    $var = $x / $y;
    print "Value of \$var is $var\n";
    
    # show the remainder...
    $rem = $x % $y;
    print "Value of \$rem is $rem\n";
    
    # division during assignment.
    $x /= $y; # $x = $x / $y
    print "Value of \$x is $x\n";
    print "Formatted as ".sprintf("%0.2f", $x)."\n";
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_9.pl 
    Value of $var is 8.33333333333333
    Value of $rem is 1
    Value of $x is 8.33333333333333
    Formatted as 8.33
    [mark@iguana perl]
    

  8. Exponents

    To get an exponent operation, the ** operator is used. For example, to get the value of 72, you would use the statement:

    print 7**2."\n";

    Some examples:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 10
    # Description: Exponent examples
    #==========================================================================
    
    # set values of the variables
    $x = 7**2;  # 49
    $y = .5;
    
    print "Value of \$x is $x\n";
    
    # square root
    $var = $x ** $y;
    print "Value of \$var is $var\n";
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_10.pl 
    Value of $x is 49
    Value of $var is 7
    [mark@iguana perl]
    

  9. Other Math Functions

    Some examples:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab05 Example 11
    # Description: Examples of other functions
    #==========================================================================
    $x = 2.5;
    
    $z = log($x);  # returns the natural log
    print "$z is the log of $x\n";
    
    $z = sin($x);  # returns the sine of $x
    print "$z is the sine of $x\n";
    
    $z = int($x);  # returns the integer value of a decimal/fractional value
    print "$z is the integer value of $x\n";
    
    $x *= -1;
    $z = abs($x);  # returns the absolute value of $x
    print "$z is the absolute value of $x\n";
    
    # parenthesis can be used to force the order of operations
    $someval = int((sin($x) - 1)/int(abs($z**3)) + 3);
    print "Someval is $someval\n";
    
    exit;
    

    When executed, the script above produces the following output:
     
    [mark@iguana perl] ./lab05_11.pl 
    0.916290731874155 is the log of 2.5
    0.598472144103957 is the sine of 2.5
    2 is the integer value of 2.5
    2.5 is the absolute value of -2.5
    Someval is 2
    [mark@iguana perl]
    

    Additional math functions can be used within perl by using the perl Math modules:

    Math::BigFloat       (3)  - Arbitrary size floating point math package
    Math::BigInt         (3)  - Arbitrary size integer math package
    Math::BigInt::Calc   (3)  - Pure Perl module to support Math::BigInt
    Math::BigRat         (3)  - arbitrarily big rationals
    Math::Complex        (3)  - complex numbers and associated mathematical functions
    Math::Trig           (3)  - trigonometric functions
    
    The use of modules will be covered in a later lesson.


last updated: 25 Sep 2014 18:01