#!/usr/bin/perl

use strict;
use warnings;

# question 1. find the length of @genes.
my (@genes, $len);
@genes = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
# don't use length! "perldoc -f length" to find out the purpose of
# length. 
$len = length(@genes);
#print("The number of genes = ", $len, "\n");
print("The number of genes = ", scalar(@genes), "\n");

# question 2. count the number of keys in %genetic_code.
my (%genetic_code);
print("The number of keys = ", scalar(keys %genetic_code), "\n");

# questions 5 and 6. create a reference to @genes. $genes_ref is a
# scalar.
my ($genes_ref);

$genes_ref = \@genes;

# what happens if you print $genes_ref.
print("Genes ref is $genes_ref\n");

# question 7. use $genes_ref to print the content of the 10th index in
# @genes. 
#$genes[9];

print("The 10th index in the array genes contains, ", 
	 $genes_ref->[9], "\n");


# question 8. 
my (@rapamycin_up, @heat_shock_up, $both_up, $only_rapamycin_up);
$len = 100;

# @rapamycin_up = (1, 2, 3, 4);
# @heat_shock_up = (3, 2, 5, 6, 7);
for my $i (0 .. 10000)
  {
    $rapamycin_up[$i] = rand();
    $heat_shock_up[$i] = rand();
    
  }

($both_up, $only_rapamycin_up) = 
  # pass the references to the two arrays.
  condition_compare(\@rapamycin_up, \@heat_shock_up);
print("There are ", scalar(@$both_up), " genes in both arrays.\n");
print("There are ", scalar(@$only_rapamycin_up), " genes only in rapamycin.\n");
# print("both_up contains ", join(" ", @$both_up), "\n");
# print("only_rapamycin_up contains ", join(" ", @$only_rapamycin_up), "\n");


sub condition_compare
  {
    # print the arguments array.
    print("Arguments are ", join(" ", @_, "\n"));
    # these variables are defined only within the scope of the
    # subroutine. 
    my ($rapamycin_up, $heat_shock_up, @both_up, @only_rapamycin_up);
    $rapamycin_up = $_[0];
    $heat_shock_up = $_[1];
    print("rapamycin_up has ", scalar(@$rapamycin_up), " values.\n");
    print("heat_shock_up has ", scalar(@$heat_shock_up), " values.\n");
    # print("Values in rapamycin_up are ", join(" ", @$rapamycin_up), ".\n");
    # print("Values in heat_shock_up are ", join(" ", @$heat_shock_up), ".\n");
    
    # ok, we now have the correct arguments. let us find out which
    # genes are in both $rapamycin_up and in $heat_shock_up.

    # method 1. check all pairs of genes, one in rapamycin_up and the
    # other in heat_shock_up.
    my ($rap, $hs, $found);
    foreach $rap (@$rapamycin_up)
	 {
	   # we have not seen this value of $rap in @$heat_shock_up.
	   $found = 0;
	   foreach $hs (@$heat_shock_up)
		{
		  if ($rap eq $hs)
		    {	
			 push(@both_up, $rap);
			 $found = 1;
			 last;
		    }
		}
	   # only after looping over all elements in @$heat_shock_up,
	   # should we check if we have seen $rap.
	   if (!$found)
		{
		  push(@only_rapamycin_up, $rap);
		}

	 }
    print("There are ", scalar(@both_up), " common genes.\n");
    print("There are ", scalar(@only_rapamycin_up), " genes only in rapamycin_up.\n");
    # remember to return references!
    return(\@both_up, \@only_rapamycin_up);
  }
