-----Question 1----- #!/usr/bin/perl -w # Title: final_problem1.pl use strict; #Create and initialize anonymous array. my $a=[1..10]; #Use the reference in a for loop to print elements, # one number per line for (my $x=0; $x<=$#$a; $x++){ print "$a->[$x]\n"; } -----Question 2----- #!/usr/bin/perl -w #Title: final_problem2.pl #Create and initialize an anonymous hash with 5 elements. my $h={ "sea otter"=>"Perl for System Administrators", "llama"=>"Learning Perl", "owl"=>"Mastering Regular Expressions", "camel"=> "Programming Perl", "bighorn sheep"=>"Perl Cookbook"}; # Use a reference in a foreach loop to print all keys and values. foreach my $animal ( keys %$h){ print "Animal on cover of $h->{$animal}:\t$animal\n"; } -----Question 3----- #!/usr/bin/perl -w #Title: final_problem3.pl use strict; # Use this module use SimpleClass; # Create the object from class my $a = SimpleClass->new(); # assemble an array to print out the words in order. my @hello_world=(W2,SP,W1,NL) ; # use a foreach loop to use the message method to print "Hello World" foreach (@hello_world){ $a->message( $_ ); } -----Question 4----- #!/usr/bin/perl #Title: final_problem4.pl package final_problem4; # final_problem4a.pl -w use strict; my $ar= [ 3.4, 2.0, -0.5, 17, -0.51, 0.3542 ]; my ($min,$i)=ArrayMin( $ar ); print "\$min=$min\t\$i=$i\n"; my ($max,$i)=ArrayMax( $ar ); print "\$max=$max\t\$i=$i\n"; # problem 4a: find the min. sub ArrayMin{ my $ar_unsorted = shift; #get the min my @ArrayMinFellers = sort { $a <=> $b } @$ar_unsorted ; my $min = $ArrayMinFellers[0]; my $k=""; #determine which element of the original array has it for ( $a=0; $a <= $#$ar_unsorted; $a++ ){ if ( $ar_unsorted->[$a] =~ m/^$min$/ ){ $k=$a; last; } } return ($min,$k); } # problem 4b: find the max. sub ArrayMax{ my $ar_unsorted = shift; #get the min my @ArrayMaxFellers = sort { $a <=> $b } @$ar_unsorted ; my $max = $ArrayMaxFellers[-1]; my $j=""; #determine which element of the original array has it for ( $a=0; $a <= $#$ar_unsorted; $a++ ){ if ( $ar_unsorted->[$a] =~ m/^$max$/ ){ $j=$a; last; } } return ($max,$j); } -----Question 5----- #!/usr/bin/perl -w #Title: final_problem5.pl use strict; # Check to see if it was called with an argument if ( @ARGV eq 0 ){ die "Usage:\t$0\tdata_file\n"; } my $data_file = shift ; my ( @dates,@mins, @maxs); print "Check this file: $data_file\n"; # open data and populate 3 arrays (dates, mins, and maxs) open (DATA,"<$data_file") or die "Could not open $data_file: $!\n"; while (){ chomp( my ($date, $min, $max) =split (/\t/, $_)); $date =~ tr/:/-/; push (@dates, $date ); push (@mins, $min ); push (@maxs, $max ); } # 1. find the minimum temp in the file. my ($min,$i)=ArrayMin( \@mins ); print "The lowest temperature of the year was $min Fahrenheight.\n"; print "The lowest temperature was recorded on $dates[$i].\n\n"; # 2. find the maximum temp in the file. my ($max,$j)=ArrayMax( \@maxs ); print "The highest temperature of the year was $max Fahrenheight.\n"; print "The highest temperature was recorded on $dates[$j].\n\n"; # -----Question 6----- Back ticks return the output from the shell command, which can be assigned to a variable. The system() operator dumps the output from the shell command to and returns the exit status of the shell program to the perl program. Example of back ticks: perl -e 'chomp (@ps=`ps`);foreach (@ps){print "My ps- $_\n";}' >>>Results in a ps list for the current uid. Example of system () operator: perl -e '$ps=system(ps);print "My ps- $ps\n";' >>>Results in a ps dump to the screen, followed by a printout of the exit status of the shell command ( zero ). If one wished to process the data resulting from the shell command, the back ticks are better, because the data can modified, and more selectively sent to the screen. system() operator would be handy if the the perl program should change it's behavior based on the exit status of the command. -----Question 7----- #!/usr/bin/perl -w #Title: final_problem7.pl use strict; if ( @ARGV eq 0 ){ die "Usage:\t$0\tdata_file\n"; } my $data_file = shift ; # open data. open (DATA,"<$data_file") or die "Could not open $data_file: $!\n"; my %phonebook; #Split the data into hash of arrays while (){ chomp( my ($name, $number) = split (/:/, $_)); #Perl cookbook recipe 276-277: Hash of Arrays push @{ $phonebook{$number}},"$name" ; } #print the results for my $phone (keys %phonebook){ print "$phone:",join(", ", @{ $phonebook{$phone}}),"\n"; }