Thursday, March 21, 2013

Perl

Language: Perl

Purpose:

Various book exercises from Modern Programming Languages: A Practical Introduction, by Adam Brooks Webber to be exposed to the high level, general purpose, interpreted (not compiled), dynamic programming language Perl.

Note: I never got grades back for this project but I assume I did well since I passed the class with an A. The program has legal syntax and should run just fine.

 


  # 1.    Write a program that computes 
   #the circumference and area of a circle 
   #(use 3.141592654 as the value of pi). 
   #Prompt for input and read the radius from standard input.
   
      

   print "Circumference of a Circle";
   $pi = 3.141592654;
   print "Please enter a radius\n";
   $radius = <STDIN>;
   print "Circumference of a circle with the radius of $radius is:\n 2*$pi*$radius";
   

 # 2.    Write a program that prompts 
 #for a string and a number on separate 
 #lines. Print the string the number 
 #of times indicated by the input number.
   

   print "Please type a String.\n";
   $theString = <STDIN>;
   print "Now enter the number of times \"$theString\" is to be written.\n";
   $numOfTimes = <STDIN>;
   

   if($theString eq "\n"){
    print "Empty string.";
   }
   else{
    foreach(1..$numOfTimes){
        print "$theString";
    }
   }
   

   #3. Write a program that reads a list 
   #of numbers from standard input 
   #until an end-of-file (ctrl-z). 
   #Print the corresponding name 
   #for each number in the input 
   #list.
   

   @flinstones = qw / Fred Wilma Barney Betty Dino Pebbles BamBam /;
   

   $index = 0;
   while($line ne eof){
    $line = <STDIN>;
    print "\n";
    $numList[index] = $line;
    index++;
   }
   index = 0;
   $numListLength = @numList;
   

   foreach (1..$numListLength){
   

    if($numList[index] > numListLength){
        print "No such index: $numbList[index]\n";
    }
    else{
        print "$flinstones[$numbList[index]]\n";
    }
   index++;
   

   }
   

   #Project 2
   

   #1.    Write a subroutine called &total 
   #that returns the total of a list of 
   #numbers. The list is to be the input, 
   #the answer is to be returned.
   
   

   
    my @numbers = qw( 1 3 5 7 9 );
    my $numberTotal = &total(@numbers);
    print “The total of \@numbers is $numberTotal\n”;
    print “Enter some numbers on separate lines: ”;
    my newTotal = &total(<STDIN>);
    print “The total of \@numbers is $numberTotal\n”;



No comments:

Post a Comment