#!/usr/bin/perl
use strict;
use warnings;

#1) Declare an array containing a list of numbers. Write code that produces the sum of the numbers. Determine the maximum value in the list (find out how to sort numerically) .

my @numbers = (1, 2, 19, 4, 5, 10, 20, 3, 2, 25, 33, 2, 7, 5 ,9);
my $sum;
foreach my $number (@numbers) {
$sum = $sum + $number;
}

print "sum is $sum \n";

my @sorted = sort { $a <=> $b } @numbers ;
my $max = pop (@sorted);

print "max value is $max \n";

#2) Declare a hash with countries as the hash key and capitals as the hash value.

my %capitals = (
    Spain  => "Madrid" ,
    France => "Paris",
    Japan  => "Tokyo",
    Chile  => "Santiago",
    Egypt  => "Cairo"
    );
#Write a small script that lists the countries in alphabetical order along with their capitals.

foreach my $country ( sort (keys %capitals) ) {
    print "$country: $capitals{$country} \n" ;
}

#3) Declare two hashes, similar to the one in 2, but with different keys /values. Write a script that combines the two hashes into a new hash.

my %more_capitals = (
    'Costa Rica' => "San Jose",
    Sweden => "Stockholm",
    Uganda => "Kampala"
    );

foreach my $country (keys %more_capitals) {
    $capitals{$country} = $more_capitals{$country};
}

print "\n Combined hash is\n";
foreach my $country ( sort (keys %capitals) ) {
    print "$country: $capitals{$country} \n" ;
}
#4) Take the list defined in 1) and transform it into another list containing the squares of the values. Implement this using the foreach method and again using the map function.

my @squares = map { $_ ** 2 } @numbers;

print "numbers are \n" .  join(', ' , @numbers) . "\n squares are \n" . join(', ', @squares) . " \n";

foreach my $number (@numbers) {
    $number = $number ** 2 ;
}

print "squares of numbers using foreach loop \n" . join(', ' , @numbers) . " \n";


