#!/usr/bin/perl -w # ex2_7.pl # solution to Exercise 2.7 from Deitel # @Natasha Khuri print "Please, enter the first number: "; $num1 = ; chomp($num1); print "Please, enter the second number: "; $num2 = ; chomp($num2); $sum = $num1 + $num2; $product = $num1 * $num2; $diff = $num1 - $num2; $quot = $num1/$num2; print "The sum of two numbers is ", $sum, "\n"; print "The product of two numbers is ", $product, "\n"; print "The difference of two numbers is ", $diff, "\n"; print "The quotient of two numbers is ", $quot, "\n"; exit; ------------------------------------------------------ #!/usr/bin/perl -w # ex2_9.pl # @Natasha Khuri print "Input a five-digit number: "; $number = ; chomp($number); $fifth = chop($number); $fourth = chop($number); $third = chop($number); $second = chop($number); $first = chop($number); print $first, "\n"; print $second x 2, "\n"; print $third x 3, "\n"; print $fourth x 4, "\n"; print $fifth x 5, "\n"; exit; --------------------------------------- #! /usr/bin/perl -w # ex2_11.pl # @Natasha Khuri print "Enter the first string: "; $string1 = ; chomp($string1); print "Enter the second string: "; $string2 = ; chomp($string2); if ($string1 lt $string2) { print "$string1 $string2\n"; } if ($string2 lt $string1) { print "$string2 $string1\n"; } if ($string1 eq $string2) { print "$string1\n$string2\n"; } exit; ------------------------------------------------------ #!/usr/bin/perl -w # ex2_12.pl # Solution to Exercise 2.12 from Deitel # @Natasha Khuri print "Enter a number between 1 and 30: "; $number1 = ; chomp($number1); print "Enter a number between 1 and 30: "; $number2 = ; chomp($number2); print "Enter a number between 1 and 30: "; $number3 = ; chomp($number3); print "Enter a number between 1 and 30: "; $number4 = ; chomp($number4); print "Enter a number between 1 and 30: "; $number5 = ; chomp($number5); print "*" x $number1, "\n"; print "*" x $number2, "\n"; print "*" x $number3, "\n"; print "*" x $number4, "\n"; print "*" x $number5, "\n"; exit; ------------------------------------------- #! /usr/bin/perl -w # ex3_10.pl # Solution to Exercise 3.10 from Deitel # @Natasha Khuri $total_miles = 0; $total_gallons = 0; print "Enter the gallons used (-1 to end): "; $gallons = ; chomp ($gallons); while ($gallons != -1) { print "Enter the miles driven: "; $miles = ; chomp($miles); print "The miles / gallon for this tank was ", $miles/$gallons, "\n"; $total_gallons += $gallons; $total_miles += $miles; print "Enter the gallons used (-1 to end): "; $gallons = ; chomp ($gallons); } unless ($total_gallons == 0) { print "The overall average miles / gallon was: ", $total_miles / $total_gallons, "\n"; } exit; ----------------------------------------------------------- #!/usr/local/bin/perl -w # ex3_13.pl # problem 3.13, page 93 # converting a binary number to decimal # @Deitel print( "Input a binary number: " ); chomp( $binary = ); $bit = 0; $decimal = 0; $x = $binary / ( 10 ** $bit ); while ( $x >= 1 ) { $digit = $x % 10; $decimal += $digit * 2 ** $bit; $bit++; $x = $binary / ( 10 ** $bit ); } print( "The decimal representation for $binary is $decimal.\n" ); exit; ------------------------------------------------------------------- #!/usr/local/bin/perl -w # ex3_14a.pl # problem 3.14a, page 93 # computing the factorial of a number #@Deitel print( "Enter a number: " ); chomp( $number = ); $factorial = 1; $i = $number; while ( $i > 0 ) { $factorial *= $i; $i--; } print( "$number! = $factorial\n" ); exit; ---------------------------------------------------------------- #!/usr/local/bin/perl -w #ex4_5.pl #Deitel problem 4.5 p. 134 #@Natasha Khuri @array = (1 .. 9); %hash = ( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine'); print "\@array contents are @array\n"; print "\%hash contents are\n"; while ( ( $key, $value ) = each (%hash)) { print "$key => $value\n"; } exit; -------------------------------------------------------------- #!/usr/local/bin/perl -w #ex4_6.pl #Deitel Exercise 4.6 p. 134 #@Natasha Khuri @array = (1 .. 10); %hash = @array; @keys = keys (%hash); print "\%hash keys are @keys\n"; @values = values (%hash); print "\%hash values are @values\n"; exit; ----------------------------------------------------- #!/usr/loca/bin/perl -w #Deitel exercise 4.7 p. 134 #@Natasha Khuri @deck = (1 .. 52); #create the deck of cards print "The deck of cards is\n"; print "@deck\n"; print "\nShuffling.............\n\n"; @first_half = @deck[0 .. 25]; #slice half of @deck array @second_half = @deck[26 .. 51]; #slice the remainder of @deck @deck = (); #empty @deck #longer version while( @first_half ) { $x = shift(@first_half); #retrieve first element of @first_half push(@deck, $x); #add it to end of @deck $y = shift(@second_half); #retrieve first element of @second_half push(@deck, $y); #add it to end of @deck } #shorter version #while ( @first_half ) { # push( @deck, shift @first_half ); # push( @deck, shift @second_half ); #} print "The deck of cards after shuffling is\n"; print "@deck\n"; exit; ---------------------------------------------------------- #!/usr/local/bin/perl -w #ex4_8.pl #Deitel Exercise 4.8 p.134 #@Natasha Khuri @array = (10 .. 20); #note array is sorted in decreasing order @sorted = sort {$b <=> $a} @array; $sum = 0; $num_elements = scalar (@array); while (@array) { $sum += pop(@array); } print "The size of the array is $num_elements\n"; $average = sprintf("%.2f", $sum/$num_elements); print "The sum of array elements is $sum\n"; print "The average of array elements is $average\n"; print "The five largest elements are: "; $#sorted = 4; # shortened the array to contain 5 elements print "@sorted\n"; exit; -------------------------------------------------------- #!/usr/local/bin/perl -w #ex4_9.pl #solution to problem 4.9 p.134 #@Natasha Khuri #@array = (1, 10.2, 'stuff', 'peanut', 1, 2, 15.4, 2, 4.46, 'peanut'); # or for the quiz try the following array: #@array = ('a' .. 'z', 'a', 'c'); while(@array) { #note that since the order in which #elements are removed does not matter #it is better to use pop $element = shift(@array); if (exists ($hash{$element} )) { print "$element is duplicated\n"; } else { $hash{$element} = 1; } } exit; ---------------------------------------------------------------- #!/usr/bin/perl -w #ex5_6.pl #solution to Exercise 5.6 p. 169 #@Natasha Khuri print "Enter a non-negative integer: "; $number = ; chomp ($number); if($number < 0) { print "Invalid integer\n"; print "Enter a non-negative integer: "; $number = ; chomp ($number); } if($number == 0) { $factorial = 1; } else { $factorial = 1; foreach $x (1 .. $number) { $factorial *= $x; } } print "$number! is $factorial\n"; exit; ------------------------------------------------------------- #!/usr/local/bin/perl -w #ex5_9.pl #Solution to Exercise 5.9 p. 169 #@Natasha Khuri for ( $i = 0; $i < 10; $i++ ) { print( "Enter a number: " ); chomp( $number = ); push( @array, $number ); } print( "\n" ); #part a: foreach structure print "Using foreach structure:\n"; foreach $element (@array) { $square = $element ** 2; print "$square\n" if ( ($square >= 100) && ($square <= 200)); } #part b: grep function print "Using grep and map functions:\n"; @squared_numbers = map ($_ ** 2, @array); @result = grep ( (($_ >=100) && ($_ <= 200)), @squared_numbers); #shorter version @results = grep ( $_ >= 100 && $_ <= 200, map ( $_ ** 2, @array ) ); $" = "\n"; #reset the special variable to output each element #of @result on a new line print "@result\n"; exit; -------------------------------------------------------------------- #! /usr/bin/perl -w # ex5_10.pl # Solution to Exercise 5.10 from Deitel # @Natasha Khuri $total_miles = 0; $total_gallons = 0; for ($i=0; ;++$i) { print "Enter the gallons used (-1 to end): "; $gallons = ; chomp ($gallons); last if ($gallons == -1); print "Enter the miles driven: "; $miles = ; chomp($miles); redo if ($gallons < 0 or $miles < 0); print "The miles / gallon for this tank was "; print $miles/$gallons, "\n\n"; $total_gallons += $gallons; $total_miles += $miles; } unless ($i == 0) { print ("The overall average miles / gallon was: "); printf ("%.2f\n", $total_miles / $total_gallons); print ("The average miles / tank was: "); printf ("%.2f\n", $total_miles / $i); print ("The average gallons per fillup was: "); printf ("%.2f\n", $total_gallons / $i); } exit; ---------------------------------------------------------------------- #!/usr/bin/perl -w # ex6_4.pl #solution to exercise 6.4 p. 175 #@Natasha Khuri num_arguments(); num_arguments(1); num_arguments("one", "two", "three"); sub num_arguments { print "Number of arguments passed: "; print scalar (@_), "\n"; } ---------------------------------------------------------------- #!/usr/bin/perl -w # ex6_6.pl #solution to exercise 6.6 p. 215 #@Natasha Khuri %my_hash = roll(6000); print "Outcome\tFrequency\n"; while ( ($key, $value)= each(%my_hash)) { print "$key\t$value\n"; } sub roll { $times = shift(@_); srand(time|$$); # not really needed foreach ( 1 .. $times) { $side = 1 + int (rand(6)); ++ $result_hash{$side}; } return %result_hash; } exit; ----------------------------------------------------------- #!/usr/local/bin/perl -w #ex6_7.pl #solution to Exercise 6.7 # @Deitel use strict; my @numbers = ( 1, 7, 3, 8, 4, 2, 45, 53, 1, 3 ); print( "Maximum recursively is: ", recursiveMaximum( @numbers ), "\n" ); print( "Maximum iteratively is: ", iterativeMaximum( @numbers ), "\n" ); sub recursiveMaximum { my @numbers = @_; if ( @numbers == 1 ) { return shift( @numbers ); } else { my $first = shift( @numbers ); my $max = recursiveMaximum( @numbers ); return ( $first > $max ) ? $first : $max; } } sub iterativeMaximum { my @numbers = @_; my $max = shift( @numbers ); foreach ( @numbers ) { $max = ( $max > $_ ) ? $max : $_; } return $max; } exit; ----------------------------------------------------------- #!/usr/bin/perl # Ex. 6.8: HTMLpackage.pm # Package of subroutines for creating HTML markup. package HTMLPackage; our $title = ''; sub startDocument { return "$title\n"; } sub createParagraph { return "

@_

\n"; } sub createHyperlink { return "$_[ 0 ]\n"; } sub endDocument{ return "\n"; } return 1; ----------------------------------------------------------- #! /usr/local/bin/perl -w # problem 6.9, page 215 # Demonstrating the different ways # of calling localtime. #@Natasha Khuri use strict; my $scalar = get_time(); my @array = get_time(); print( "Calling localtime in a scalar context:\n" ); print( "$scalar", "\n\n" ); print( "Calling localtime in a list context:\n" ); print( "@array", "\n" ); sub get_time { if (wantarray()) { return localtime(); } else { return localtime(); } } exit; ----------------------------------------- #!C:\Perl\bin\perl.exe -w #ex7_7.cgi #solution to Exercise 7.7 p. 250 #Using CGI.pm to determine the properties of a triangle. #@Deitel use warnings; use strict; use CGI qw( :standard ); print( header() ); print( start_html( "Triangles" ) ); my $hypotenuse = param( "Hypotenuse" ); my $shorter = param( "Short" ); my $longer = param( "Long" ); if ( $hypotenuse && $shorter && $longer ) { if ( $hypotenuse < $shorter || $hypotenuse < $longer || $longer < $shorter ) { print( "Please re-enter the three sides ", "in the correct places: " ); ourform(); } elsif ( $hypotenuse >= $shorter + $longer ) { print( "Those three sides do not make a triangle, ", "please reenter: " ); ourform(); } else { print( "The triangle with the sides $hypotenuse, ", "$shorter, and $longer is " ); if ( $hypotenuse == $shorter && $hypotenuse == $longer ) { print( "an equilateral triangle." ); } else { if ( $hypotenuse == $longer || $shorter == $longer ) { print( "an isocolese " ); if ( $hypotenuse ** 2 == $longer ** 2 + $shorter ** 2 ) { print( "and a right triangle." ); } else { print( "triangle." ); } } elsif ( $hypotenuse ** 2 == $longer ** 2 + $shorter ** 2 ) { print( "a right triangle." ); } else { print( "not special." ); } } } } else { print( "Please enter the three sides of a triangle:\n" ); ourform(); } print( end_html() ); sub ourform { print( start_form(), "Hypotenuse ", textfield( -name => "Hypotenuse" ), br(), "Longer side ", textfield( -name => "Long" ), br(), "Shorter side ", textfield( -name => "Short" ), br(), submit( "Submit" ), end_form() ); } ------------------------------------------------------------- #!C:\Perl\bin\perl.exe -w #ex7_8.cgi #solution to Exercise 7.8 p. 251 #A soothsayer program. #@Deitel use warnings; use strict; use CGI qw( :standard ); print( header(), start_html( "Soothsayer Program" ) ); if ( param ) { my @array = ( "Signs point to yes", "No", "Yes", "Ask later", "Perhaps not", "Could be", "No chance", "Never!!!!" ); print( h1( $array[ rand( 8 ) ] ) ); } else { print( h1( "Soothsayer" ), br(), p( "Enter your question here: " ), start_form(), textfield( -name => "question" ), submit(), end_form() ); } print( end_html() ); ---------------------------------------------------------- !/usr/bin/perl # Exercise 8.5: Ex08_05.pl # Convert all words to uppercase. # (c) Deitel use strict; use warnings; print( "Enter a string: " ); chomp ( my $string = ); print( capitalize( $string ) ); sub capitalize { my $string = shift(); $string =~ s/\b(\w)/\u$1/g; return $string; } ------------------------------------------------------------ #!/usr/bin/perl # Exercise 8.6: Ex08_06.pl # A regular expression to count digits, whitespace and words. # copyright Deitel use strict; use warnings; print( "Enter a string: " ); chomp ( my $string = ); my $digits = 0; $digits++ while ( $string =~ /\d/g ); my $whitespace = 0; $whitespace++ while ( $string =~ /\s/g ); my $words = 0; $words++ while ( $string =~ /\b/g ); $words /= 2; print( "It has $digits digits, $whitespace whitespace ", "and $words words.\n" ); -------------------------------------------------------------- #!/usr/bin/perl # Exercise 8.7: Ex08_07.pl # Searches for URL's in HTML. use strict; use warnings; searchURL ( "\"http://www.yahoo.com\"" ); searchURL ( "" ); searchURL ( " \"News\"\"Today\"" ); sub searchURL { my $string = shift(); while ( $string =~ m|(?<=")(http://[^"]+)|g ) { print( "URL: $1\n" ); } } ########################################################################### # (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. # # All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ########################################################################### --------------------------------------------------------------------------- #!/usr/bin/perl # Exercise 8.9: Ex08_09.pl # Determine how many html tags a certain string has. # Count for each level of nesting. # copyright Deitel use strict; use warnings; my $string = "

ab

cde"; print( "The HTML string is:\n$string\n\n" ); our @counter; count( $string, 0 ); for ( 0 .. $#counter ) { print( "There are $counter[ $_ ] valid level ", $_, " html tags.\n" ); } sub count { my $string = shift(); my $level = shift(); while ( $string =~ m!.*?<([^<]*?)>(.*?)!g ) { $counter[$level]++; print( "Tag is \"$1\", contains \"$2\" " ); print( "at level $level\n" ); count( $2,$level+1 ); } } ########################################################################### # (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. # # All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ########################################################################### -------------------------------------------------------------------------- #!/usr/bin/perl -w #solution to exercise 13.5 from Deitel #@Natasha Khuri use strict; my %hash = qw(one 1 two 2 three 3 four 4 five 5); #call hashsum with a hash reference and a reference to #an anonymous array my $result = &hashsum(\%hash, [qw (one two three) ]); print $result, "\n"; #call hashsum with a hash reference and an array reference my @array = ("four", "five"); $result = &hashsum(\%hash, \@array); print $result, "\n"; #create references and call hashsum my $ref_hash = \%hash; my $ref_ar = \@array; $result = &hashsum($ref_hash, $ref_ar); print $result, "\n"; sub hashsum { my $total = 0; my $hash_ref = shift (@_); my $array_ref = shift (@_); foreach (@$array_ref) { $total += $$hash_ref{$_} if exists ($$hash_ref{$_}); } return $total; } exit; -----------------------------------------------------------