#!/usr/bin/perl

#
# Summary of chapter3
#

###############
### Scalars ###
###############

$a = 4;
$b = 'a string';
$c = "Another string";

print $a, $b, $c, "\n";
$ans = <>;

print "$a $b $c\n";
$ans = <>;

print '$a $b $c\n';
$ans = <>;

$c = "How to mix $b up a bit! " . $c . " \" ' \" ";

print "$c\n"; 
$ans = <>;

##############
### Arrays ###
##############

@arr = (1, 2, 'three', 55, 'Perl', -7);

print "First element: $arr[0], third element: $arr[2]\n"; 
$ans = <>;

print "@arr\n";
$ans = <>;

push @arr, (-1, -2, -3);
print "@arr\n";
$ans = <>;

$last_element = pop @arr;
print "Last element was: $last_element\n";
print "Is is now removed from the array: '@arr'\n";
$ans = <>;

$len = @arr;
$last_index = $#arr;
print "Length is: $len, last index is $last_index\n";
$ans = <>;

##############
### Hashes ###
##############

%h1 = ('key1', 'val1', 'key2', 'val2');
%h2 = ('key1' => 'val1', 'key2' => 'val2');
print "%h1\n";
print %h2,"\n";
$ans = <>;

print "'key1' has the value: $h1{'key1'}\n";
$ans = <>;

@allkeys = keys %h1;
@allvalues = values %h1;
print "@allkeys\n";
print "@allvalues\n";
$ans = <>;

@arr = %h1;
print "@arr\n";
$ans = <>;

delete $h1{'key1'};
print %h1, "\n";
$ans = <>;

##################
### Conditions ###
##################

$a = 5;
print "The scalar \$a is smaller than 10\n" if ($a < 10);

if ($a < 0) {
   print "Scalar negative!\n";
} elsif ($a >= 5 && $a <= 10) {
   print "Scalar in [5,10]\n";
} else {
   print "The scalar is $a\n";
}

$a = 4;
unless ($a == 5 || $a > 10) {
   print "Puhh!\n";
}

#############
### Loops ###
#############

# foreach
@nums = (1,2,3,4,3,4,5,6);
$cnt = 0;

foreach $num ( @nums ) {
   if ($num == 4) {
      $cnt++;
   }
}
print "Found $cnt 4's in the array\n";
$ans = <>;

$check = 5;
foreach $num ( @nums ) {
   if ($num == $check) {
      print "The array contains at least one $check\n";
      last;
   }
}
$ans = <>;

# for
$N = 100;
$sum = 0;
for ( $i = 1; $i <= $N; $i++) {
   $sum += $i;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

$sum = 0;
for ( $i = 1; $i <= $N; $i += 2) {
   $sum += $i;
}
print "The sum of all odd integers 1..$N is $sum\n";
$ans = <>;

$sum = 0;
for ( $i = 1; $i <= $N; $i++) {
   next if ($i % 2 == 0);
   $sum += $i;
}
print "The sum of all odd integers 1..$N is $sum\n";
$ans = <>;

# while
print "From the keyboard!\n";
while ($line = <>) {
   print "Echo: $line";
}
$ans = <>;

$sum = 0;
$i = 1;
while ($i <= $N ) {
   $sum += $i; 
   $i++;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

@arr = (1,3,4,0,2,3);
while ( @arr ) {
   print shift @arr, "\n";
}
$ans = <>;

###########################
### Regular expressions ###
###########################
$a = 'test of some  basic regular expressions';

if( $a =~ /test/ ) {
   print "\$a contains 'test'\n";
}
$ans = <>;

if( $a =~ /\b\w+\b/ ) {
   print "\$a contains a word\n";
}
$ans = <>;

if( $a =~ /\s{2}/ ) {
   print "\$a contains double spaces\n";
}
$ans = <>;

$b = '1234 More tests 23 Tjohej Frodo';
if( $b =~ /^\d+\s.*\s\w+o$/ ) {
   print "There was a match in \$b!'\n";
}
$ans = <>;

$a = 
'AGCTTTTCATTCTGACTGCAACGGGCAATA' . 
'TTCTGAACTGGTTACCTGCCGTGAGTAAAT' . 
'TACAGAGTACACAACATCCATGAAACGCAT';

if( $a =~ /CAT[GA]/ ) {
   print "\$a contains 'CATG or CATA'\n";
}
$ans = <>;

$cnt = 0;
while( $a =~ /CAT[GA]/g ) {
   $cnt++;
}
print "Found $cnt occurences of CATG/CATA\n";
$ans = <>;

while( $a =~ /(A{2}(\w{2}))/g ) {
   print "$1, $2\n";
}

####################
### File handles ###
####################
open $FH, '<', 'RomeoAndJuliet.txt' or die 'Something went wrong $!';

my $lady = 0;
my $wordCnt = 0;
my $debug = 0;
while ($line = <$FH>) {
   $wordCnt += split ' ', $line;

   while ($line =~ /([Ll]ad(y|ies)'?)/g) {
      $lady++;
      print "$1\n" if ($debug);
   }
}
print "The book contains about $wordCnt words\n";
print "Found $lady 'lady' words in the book!\n";
