#!/usr/bin/perl 
# Title:        2.16Exercises.pl
# Purpose:   	Simple calculation, control structures,
#	 	<STDIN> input,  data checking, sting, arrays 

use Term::ANSIColor;

############################################################
#  BEGIN: Clear the screen
############################################################
#----------------------------------------#
#   Method 1 Perl Cookbook, pg 521 	 #
# 	Uses Term::Cap to send sequence  #
# 	for the specific terminal        #
#----------------------------------------#
use Term::Cap ;
$OSPEED=9600;
eval {
	require POSIX;
	my $termios=POSIX::Termios->new();
	$termios->getattr;
	$OSPEED= $termios->getospeed;
};
$terminal = Term::Cap->Tgetent({OSPEED=>$OSPEED});
$terminal->Tputs('cl',1,STDOUT);
#-----------------------------------#
#   Method 2 Perl Cookbook, pg 521  #
#	call the system function    #
#-----------------------------------#
system("clear");
#---------------------------------------#
#   Method 3 Perl Cookbook, pg 521  	#
# 	If clearing the screen a lot,   #
#     	cache the return from termcap,  # 
#     	and print the new screen char.  #
#---------------------------------------#
$clear=$terminal->Tputs('cl');
$clear= `clear`;
print $clear;
############################################################
#  END: Clear the screen
############################################################
#  BEGIN: PauseBlock
#  Ask for user to press enter, then clear screen.
############################################################
sub PauseBlock
{
print "\nPress ENTER to continue\n";
$bla=<STDIN>;
print $clear;
}
############################################################
#  END: PauseBlock
############################################################
#  BEGIN: InputArray  function
#   Asks for input, returns @input_array
############################################################
sub InputArray
{
	print "Enter a list of strings,\n";
	print "press ctrl-d when finnished.\n\n";
 	@input_array=<STDIN>; chop(@input_array); 
 	$input_array=@input_array;  #calculate the number of elements in array
 	ValidArray();
}
############################################################
#  END: InputArray function
############################################################
#  BEGIN: ValidArray  function
#   If input was an array, return the input.
############################################################
sub ValidArray
{
if ( $input_array gt 0 )
  {
  	@valid_array=@input_array;
        return;
   } else {
        print color("red"),"You must enter at least one line.\nSorry.\n", color("reset"); 
	exit 0;      
   }
}
############################################################
#  END: ValidArray function
############################################################


print "###############################################################\n";
print "#  Assignment 2 Problem 1.  \n";
print "#  Read a list of strings and print them out in reverse order.\n\n";
#-------------------
#  Input the array
#-------------------
InputArray();
#-----------------------------------
#  Validate input array 
#     Takes    @input_array 
#     Returns: @valid_array
#-----------------------------------
ValidArray();
#-----------------------------------
# Create an array in reverse order:
#-----------------------------------
my @reverse_array=reverse( @valid_array );
#-----------------------------------
#  Output
#-----------------------------------
#print "\n\nOutput result with print:\n";
#print "\t@reverse_array\n";
print "\nOutput result with a for loop:\n";
for ( $i=0; $i < @reverse_array; $i++){
	print color("blue"),"\tElement $i contains:\t",
	      color("green on_blue blink bold"),$reverse_array[$i],".\n",
	      color("reset");
}

# print "\nOutput Result: Try foreach loop\n";
# $i=0;
# foreach $line (@b) {
# 	print "\tReverse Array element\t",$i," contains:\t",$line,".\n";
# 	$i++;
# }

PauseBlock ();

print "\n#############################################################\n";
print "#  Assignment 2 Problem 2. \n";
print "#  Read a number (n) and then a list of strings. \n";
print "#  Print the list that corresponds to the number.\n\n";
#-----------------------------------
#  Input a number
#-----------------------------------
print "Enter a number greater than 0, and press Enter.\n";
print "The number should be less than the number of lines you type \n";
$line_number=<STDIN>; chop($line_number);
#-----------------------------------
#  Input the array
#-----------------------------------
InputArray();
#-----------------------------------
#  Validate input array 
#     Takes    @input_array 
#     Returns: @valid_array
#-----------------------------------
ValidArray();
#-----------------------------------
# did they enter neough elements for 
# the number typed?
#-----------------------------------
$actual_lines=$#valid_array+1;
if ( $line_number gt $actual_lines ){	
	print color("red"),"Yout entered  $actual_lines lines, and want me to print out line number $line_number.", color("reset");
	print color("red"),"Pick a smaller number or type in more lines.  Exiting.\n", color("reset");
	exit 0;
}

#-----------------------------------
#  Output the line they entered
#-----------------------------------
print "\nOutput Result:\n";
print color("blue"),"\tLine $line_number contained:\t",color("green on_blue blink bold"),$valid_array[$line_number],"\n", color("reset");

PauseBlock ();
print "\n#############################################################\n";
print "#  Assignment 2 Problem 3.  \n";
print "#  Read a list of strings,\n";
print "#  then pring one at random. \n";
print "#  \tEquation:\t  randoomnumber=srand(somearray) \n";
#-----------------------------------
#  Input the array
#-----------------------------------
InputArray();
#-----------------------------------
#  Validate input array 
#     Takes    @input_array 
#     Returns: @valid_array
#-----------------------------------
ValidArray();
#-----------------------------------
# Generate a random number from 0 to last index in array,
# Use 'int' to force the number to be a whole number.
#-----------------------------------
srand;
$b=int(rand(@valid_array));
#-----------------------------------
#  Print Output
#-----------------------------------
print color("blue"),"\nRandomly chose line $b:\t",color("green on_blue blink bold"),$valid_array[$b],"\n", color("reset"),"\n";

PauseBlock ();
print "\n#############################################################\n";
print "#  Assignment 2 Problem 4.  \n";
print "#  Use a for loop, assign elements of array a to b, in reverse.\n\n";
print "#  Only use non-negetive indices.\n";
#-----------------------------------
# declare array a for next 4 problems
#-----------------------------------
@a=(1,2,3,4,5,6,7,8,9,10);    # use constructor opperator to populate array
@b=();
print "Array \'a\' now contains:\t@a\n";
print "dolar pound a contains:\t",$#a,"\n";
#-----------------------------------
# populate Array b with reverse of array a, using a for loop and @a.
#-----------------------------------
for ( $i=$#a, $j=0; $i>-1; $j++, $i--){
	$b[$j]=$a[$i];
	print 	color("blue on_black"),"Element b\[",color("reset"),
		color("green on_blue blink bold"),$j,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$b[$j],color("reset"),
		color("blue on_black "),"\t and a\[",color("reset"),
		color("green on_blue blink bold"),$i,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$a[$i],color("reset"),"\n";
}
PauseBlock ();
print "\n#############################################################\n";
print "#  Assignment 2 Problem 5.  \n";
print "#  Repeat Problem 4, using negetive indices \n\n";
for ($i=-1,$j=-$#a;$j<1;$i--,$j++){
	$b[$j]=$a[$i];
	print 	color("blue on_black"),"Element b\[",color("reset"),
		color("green on_blue blink bold"),$j,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$b[$j],color("reset"),
		color("blue on_black "),"\t and a\[",color("reset"),
		color("green on_blue blink bold"),$i,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$a[$i],color( "reset" ),"\n";
}
PauseBlock ();
print "\n#############################################################\n";
print "#  Assignment 2 Problem 6.  \n";
print "#  Repeat Assignment 2.4, using the reverse opperator \n\n";
@b=reverse(@a); $j=0;$i=$#a;
foreach $element (@b){ 
	print 	color("blue on_black"),"Element b\[",color("reset"),
		color("green on_blue blink bold"),$j,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$element,color("reset"),
		color("blue on_black "),"\t and a\[",color("reset"),
		color("green on_blue blink bold"),$i,color("reset"),
		color("blue on_black "),"\] = ",color("reset"),
		color("green on_blue blink bold"),$a[$i],color("reset"),"\n"; 
	$i--; $j++;
}	
PauseBlock ();
print "\n#############################################################\n";
print "#  Assignment 2 Problem 7.  \n";
print "#  Repeat Assignment 2.4, using any combination of shift, unshift, pop and push.\n\n";

$j=0; @b=();
print "\n",color("red"),"Using push in a foreach loop:",color("reset"),"\n";
foreach $element (@a){	
	print color("blue"),"Element b\[$j\] = ",$element,"\t and a\[",$#a-$j,"\] = ",$a[$#a-$j],color("reset"),"\n";
	push ( @b, $element );
	$j++;
}

print "\n",color("red"),"Using pop inside push in a while \$\#a is greater than -1.",color("reset");
print "\n",color("red"),"Pop last element of \@a, and push on to the last element of \@b",color("reset"),"\n";  
@b=();  #clear @b
while ( $#a gt -1){
  	print "\n\@a =\t@a\n\@b =\t@b\n";
	push (@b,pop(@a))  ;
  	#print "\$\#a = $#a\t\$a\[$#a\] = $a[$#a]\t \$\#b = $#b\t  \$b\[0\] = $b[0]   \n";
  	#Must print before # print "\n\@a =\t@a\n\@b =\t@b\n";
}  
  ## Must print out last element 
  print "\n\@a =\t@a\n\@b =\t@b\n";

print "\n",color("red"),"Using shift inside unshift in a while  \$\#b is greater than zero:",color("reset");
print "\n",color("red"),"Shift off first element of \@b, unshifting to first element of ",color("reset"),"\n";
while ( $#b gt -1){
	print color("green"),"\n\@a =\t@a",color("reset"),"\n",color("blue"),"\@b =\t@b",color("reset"),"\n";
	unshift(@a,shift(@b));
}
  ## Must print out last element 
  print color("green"),"\n\@a =\t@a",color("reset"),"\n",color("blue"),"\@b =\t@b",color("reset"),"\n";

exit 0;
