#!/usr/bin/perl
#####
#####<1>
#####@@@Scalar, array, and hash assignment
print "\n###Array and hash array beginning!!! \n";
$salary=50000;                #scalar
@months=('Mar','Apr','May');  #array
%states=(                     #hash array
 'CA' => 'California',   
 'ME' => 'Maine',
 'MT' => 'Montana',
 'NM' => 'New Mexico'
);
print "$salary\n";  #display the scalar
print "@months\n";  #traversal array without separator
print "$months[0], $months[1], $months[2]\n"; #traversal array with separator
print "$states{'CA'}, $states{'NM'}\n";     #print hash values using hash key
print "***$name***\n";  # It is a warning when you print a null scalar, if you use -w.

$number = 150;
$name = "Jody Savage";
$today = localtime();    #use the localtime function
$var="net";
print "${var}work\n";    #string connect like shell.

$name="Tommy";
print "OK \n" if defined $name;  #compare with shell:  if [-n $name ]
print  `echo Does it exists output? $name`;
undef $name;     #compare with shell:  unset name
print  `echo Does it exists output? $name`;
#####result:
####Array and hash array beginning!!!
#50000
#Mar Apr May
#Mar, Apr, May
#California, New Mexico
#******
#network
#OK
#Does it exists output? Tommy
#Does it exists output?


#####
#####<2>
#####@@@arrays initial
#qw() qw{} qw<> qw[]  ==> backquotes
print "\n###Arrays initial beginning!!!\n";
@name=("Guy", "Tom", "Dan", "Roy"); #use list to initial an array
@list=(2..10);  #grant 2 ~ 10 integer number to array @list
@grades=(100, 90, 65, 96, 40, 75);  #use list to initial an array
$a=a, $b=b, $c=c;
@items=($a, $b, $c); #use scalar to initial array.
@empty=();       #empty the array.
$size=@items;    #return the array size, counting the array size.
print `echo $size`;  # result is 3

@manuals = qw/dogs cats cows/;
@fruit = qw(apples pears peaches);
print "@manuals \n";
print "@fruit \n";
#####result:
####Arrays initial beginning!!!
#3
#dogs cats cows
#apples pears peaches


#####
#####<3>
#####@@@use $#array_name to truncate array
print "\n###use array_name to truncate array beginning!!!\n";
@grades=(90,89,78,100,87);
print "The original array is: @grades\n";
print "The number of the last index is $#grades\n"; # $#grades + 1 = length
$#grades=3;    #change the index of array.
print "The array is truncated to 4 elements: @grades\n"; #enumerate
@grades=();
print "The array is completely truncated: @grades\n";  #result: null
#####result:
####use array_name to truncate array beginning!!!
#The original array is: 90 89 78 100 87
#The number of the last index is 4
#The array is truncated to 4 elements: 90 89 78 100
#The array is completely truncated:


#####
#####<4>
#####@@@populating an array and printing its values
print "\n###populating an array and printing its values beginning!!!\n";
@names=('John', 'Joe', 'Jake');
print @names, "\n"; #enumerate array without separator.
print "Hi $names[0], $names[1], and $names[2]!\n";  #display elements of array, using scalar!!
$size=@names;   #count the size of array.
print "There are $size elements in the \@names array. \n";  #$size ==> 3
print "The last element of the array is $names[$size - 1]. \n"; #Jake
print "The last element of the array is $names[$#names].\n";  #Jake $#names return last index of array
@fruit = qw(apples pears peaches plums);   #use qw() qw[] qw{} initial array.
print "The first element of the \@fruit array is $fruit[0];
the second element is $fruit[1]. \n";
print "Starting from the end of the array: @fruit[-1, -3]\n"; #enumerate element using reverse index. 
#####result:
####populating an array and printing its values beginning!!!
#JohnJoeJake
#Hi John, Joe, and Jake !
#There are 3 elements in the @names array.
#The last element of the array is Jake.
#The last element of the array is Jake.
#The first element of the @fruit array is apples;
#the second element is pears.
#Starting from the end of the array: plums pears



#####
#####<5>
#####@@@Array slices
print "\n###Array slices beginning!!!\n";
@names=('Tom','Dick','Harry','Pete');
@pal=@names[1,2,3];   #slice -- @names[1..3] also O.K.
print "@pal\n";
($friend[0], $friend[1], $friend[2])=@names;   #Array slice
print "@friend\n";   #left <== right, values pass.
######result:
####Array slices beginning!!!
#Dick Harry Pete
#Tom Dick Harry



#####
#####<6>
#####@@@Array slices
print "\n###Array slices beginning!!!\n";
@colors=('red','green','yellow','orange');
($c[0],$c[1],$c[3],$c[5])=@colors;   #slice to another array.
print "************\n";
print @colors, "\n";  #print entire array without separate quoted.
print "@colors, \n";  #print entire array with separate quoted.
print "************\n";
print $c[0], "\n";  #red
print $c[1], "\n";  #green 
print $c[2], "\n";  #undefined
print $c[3], "\n";  #yellow
print $c[4], "\n";  #undefined 
print $c[5], "\n";  #orange
print "************\n";
print "The size of the \@c array is ", $#c + 1,"\n";
######result:
####Array slices beginning!!!
#************
#redgreenyelloworange
#red green yellow orange,
#************
#red
#green
#
#yellow
#
#orange
#************
#The size of the @c array is 6


#####
#####<7>
#####@@@two-dimensional array:
print "\n###two-dimensional array beginning!!!\n";
@matrix=([3 , 4, 10],
         [2 , 7, 12],
         [0 , 3,  4],
         [6 , 5,  9],
        );
print "@matrix\n"; #dispaly memory address of all elements of array
print "Row 0, column 0 is $matrix[0][0].\n"; #the same $matrix[0]->[0]
print "Row 1, column 0 is $matrix[1][0].\n"; #the same $matrix[1]->[0]
for($i=0; $i<4; $i++){
  for($x=0; $x<3; $x++){
    printf "%3d",$matrix[$i][$x] ;  #traversal
  }
  print "\n";
}
######result:
####two-dimensional array beginning!!!
#ARRAY(0x1055f40) ARRAY(0x1055d78) ARRAY(0x1060918) ARRAY(0x1060990)
#Row 0, column 0 is 3.
#Row 1, column 0 is 2.
#  3  4 10
#  2  7 12
#  0  3  4
#  6  5  9


#####
#####<8>
#####@@@hash array:
print "\n###Hash array introducation beginning!!!\n";
%seasons=(
  "Sp" => "Spring",
  "Su" => "Summer",
  "F"  => "Fall",
  "W"  => "Winter",
);
%days=(
  "Mon" => "Monday",
  "Tue" => "Tuesday",
  "Wed" =>  undef,
);
$days{"Wed"}="Wednesday";
$days{5}="Friday";
print %days , "\n"; #dispaly the memory unit, without separate quotes
$whichday01=$days{"Tue"}; #extract element from hash array.
$whichday02=$days{"Wed"};
print "$whichday01, $whichday02\n";
######result:
####Hash array introducation beginning!!!
#MonMondayTueTuesdayWedWednesday5Friday
#Tuesday, Wednesday



#####
#####<9>
#####@@@hash slices
print "\nHash Slices beginning here!!!\n";
%officer=("NAME" => "Tom Savage",
          "SSN" => "510-22-3456",
          "DOB" => "05/19/66"
          );
@info=qw(Marine Captain 500000);
print "@info \n";  #traversal array @info
@officer{'BRANCH','TITLE','SALARY'}=@info;
#hash slice:  key mapping values
#@officer{'BRANCH','TITLE','SALARY'} mapping @info=qw(Marine Captain 500000)
print "@officer \n";

@sliceinfo=@officer{'NAME','BRANCH','TITLE'};
print "@sliceinfo\n\n";
print "The new values from the hash slice are: \n@sliceinfo\n\n";

print "The hash now looks like this:\n";
#traversal hash array.
foreach $key('NAME','SSN','DOB','BRANCH','TITLE','SALARY')
{
  printf "Key: %-10s  Value: %-15s \n", $key, $officer{$key};
}
######result:
#Hash Slices beginning here!!!
#Marine Captain 500000
#
# Tom Savage Marine Captain
#
# The new values from the hash slice are:
# Tom Savage Marine Captain
#
# The hash now looks like this:
# Key: NAME        Value: Tom Savage     
# Key: SSN         Value: 510-22-3456    
# Key: DOB         Value: 05/19/66       
# Key: BRANCH      Value: Marine         
# Key: TITLE       Value: Captain        
# Key: SALARY      Value: 500000 



#####
#####<10>
#####@@@Nested hashes
%students=("Math"    => { "Joe"  => 100, "Joan" => 95},
           "Science" => { "Bill" => 85,  "Dan"  => 76}
          );
print "On the math test Joan got ";
print qq/$students{Math}->{Joan}. \n/;  #95
print "On the science test Bill got ";
print qq/$students{Science}->{Bill}. \n/;  #85
#####result:
#On the math test Joan got 95.
#On the science test Bill got 85.



#####
#####<11>
#####@@@Anonymous arrays as keys in a hash
print "\n###Anonymous arrays as keys in a hash!!!!\n";
%grades=("Math"    => [ 90, 100, 94 ],
         "Science" => [ 77, 87, 86],
         "English" => [ 65, 76, 99, 100 ]
         );
print %grades, "\n";   #display the memory unit.
print "The third math grade is: $grades{Math}->[2] \n"; #94
print "All of the science grades are: @{$grades{Science}} \n";
#####results:
####Anonymous arrays as keys in a hash!!!!
#ScienceARRAY(0x1e5de00)MathARRAY(0x1e35d48)EnglishARRAY(0x1e5df38)
#The third math grade is: 94
#All of the science grades are: 77 87 86


#####
#####<12>
#####@@@An array of hashes.
print "\n\nAn array of hashes beginning !!!!\n";
@stores=
(  {"Boss"      => "Ari Goldberg",
    "Employees" => 24,
    "Registers" => 10,
    "Sales"     => 15000.00,
     },
   {"Boss"      => "Ben Chien",
    "Employees" => 12,
    "Registers" => 5,
    "Sales"     => 3500.00,
    },       
);
print "The number of elements in the array: ", $#stores + 1, "\n";
for($i=0; $i<$#stores + 1; $i++){
   print $stores[$i]->{"Boss"}, "\n";
   print $stores[$i]->{"Employees"}, "\n";
   print $stores[$i]->{"Registers"}, "\n";
   print $stores[$i]->{"Sales"}, "\n";
   print "-" x 20 , "\n";
}
#####result:
#The number of elements in the array: 2
#Ari Goldberg
#24
#10
#15000
#--------------------
#Ben Chien
#12
#5
#3500
#--------------------