#!/local/bin/perl -w

use strict;


# @ARGV is the array that holds command line arguments
# this loop tests if each argument is an executable file
my $arg;

foreach $arg (@ARGV) {
  if( -x $arg ) {
    print( "$arg  is an executable file\n");
  }
}

print("------------------\n");
# an example of a for loop with if conditions

my @grades = (70, 60, 55, 88, 45, 59, 92, 74);

for( my $i = 0; $i < @grades; $i++) {
  if($grades[$i] >= 80) {
    print( "$grades[$i] is above average\n" );

  } elsif( $grades[$i] >= 60 ) {
    print( "$grades[$i] is average\n" );

  } else {
    print( "$grades[$i] is below average\n" ); 
  }
}

print("------------------\n");
# an example of a while loop

my $countdown = 10;
while($countdown > 0) {
  print( "$countdown...\n");
  sleep( 1 );
  $countdown--;
}
print( "Blast off!\n" );

print("------------------\n");

# one of the more common uses of a while loop
# open a file called "grades" for input and assign the filehandle GRADES to it
open(GRADES, "<grades") or die "Couldn't open grades\n";

my $line;
my $sum = 0;
my $count = 0;

while($line = <GRADES>) { # read a line of GRADES
  chomp($line);           # remove trailing newlines
  $sum += $line;
  $count++;
}

my $avg = $sum / $count;
print("The average grade is $avg\n");
printf(STDOUT "The average grade is %.1d\n", $avg);

