#!usr/bin/local/perl -w

# NOTE: students are not required to use strict
use strict; 

# Students instructed to initialize variables.
my $num1 = 20;
my $num2 = 13;
my $result;

my @myArray = ("Fred", 34, "Mary", 23, "Little Joe", -1);
my %myHash = (California => "Sacramento", Wisconsin => "Madison" , "New York" => "Albany");

&printUsage();  # Make first call to subroutine to print usage.

print "\n>>> ";  # Prints the loop prompt before first iteration.
while (<STDIN>)  # Main loop.  Takes input and assigns it to the 
                 # anonymous var $_.  Runs until user requests "exit".
{
  my $in = $_;   # Assign loop variable $in to value of anon. var.
                 # $in has scope strictly limited to within this loop.
  chomp($in);    # Remove extraneous "returns, line feeds, white space".

  if ($in eq "add")
  {
    $result = $num1 + $num2;
    print "$num1 + $num2 = $result\n";
  }
  
  elsif ($in eq "subtract")
  {
    $result = $num1 - $num2;
    print "$num1 - $num2 = $result\n";
  }
  
  elsif ($in eq "print array")
  {
    if (@myArray == 0)  #Test for empty array.
    {
      print "The array is empty\n";
    }
    else
    {
      for (my $i = 0; $i < @myArray; ++$i) #Loop from 0 to size of array.
      {
        print $myArray[$i] . "   ";
      }
      print "\n\n";
    }
  }
  
  elsif ($in eq "pop")
  {
    if (@myArray == 0)  
    {
      print "The array is empty\n";
    }
    else
    {
      print pop(@myArray);
    }
    print "\n\n";
  }
  
  elsif ($in eq "shift")
  {
    if (@myArray == 0) 
    {
      print "The array is empty\n";
    }
    else
    {
      print shift(@myArray);
    }
    print "\n\n";
  }
  
  # hash manipulation
  elsif ($in eq "print hash")
  {
    my @keys = keys(%myHash);
    
    if (@keys == 0)  #Test for empty @keys.
    {
      print "The hash table is empty.\n";
    }
    else
    {
      foreach my $item (@keys) #Loop for all items in @keys.
      {
        print "$item: => $myHash{$item}" . "\n";
      }
    }
  }
  
  elsif ($in eq "hash keys")
  {
    my @keys = keys(%myHash);
    
    if (@keys == 0)
    {
      print "The hash table is empty.\n";
    }
    else
    {
      foreach my $item (@keys)
      {
        print "$item\n";
      }
    }
  }
  
  elsif ($in eq "hash values")
  {
    my @values = values(%myHash);
    
    if (@values == 0)#Test for empty @values.
    {
      print "The hash table is empty.\n";
    }
    else
    {
      foreach my $item (@values) #Loop for all items in @values.
      {
        print "$item\n";
      }
    }
  }
  
  elsif ($in eq "delete")
  {
    my @keys = keys(%myHash);
    
    if (@keys == 0)
    {
      print "The hash table is empty.\n";
    }
    else
    {
      print " deleting \"$keys[0] => $myHash{$keys[0]}\"" . "\n";
      delete($myHash{$keys[0]});
    }
  }
  
  # List commands for program functionality.
  elsif ($in eq "usage")
  {
    &printUsage();  #Call for subroutine that prints usage.
  }
  
  elsif ($in eq "exit") 
  {
    exit(0); #Forces explicit exit from prog. & return
             # to shell.
  }
  
  else
  {
    print "Don't know what this means.\n";  #User error message.
  }
  
  print "\n>>> ";
}  #End of while user input loop.

sub printUsage()  #Code for subroutine that prints usage. No arguments.
{
  print "AVAILABLE COMMANDS:\n\n" .
      "add\nsubtract\n\nprint array\npop\nshift\n\n" .
      "print hash\ndelete\nhash keys\nhash values\n\n" .
      "usage\nexit\n\n";
}