Test 1 Intro to Programming in Perl out: Oct 2 2003 due: by 8:30am on Tues Oct 6 from: mahoney@marlboro.edu -- Rules --------------------------------------------- Do all six problems. Send me your results in an email as usual. You may use your text or other references, including running anything you please in a perl program. Don't use other people. Please do say what sources you used for each problem. Include appropriate comments, tests, and diagnostic output for your code : your goal is to make it easy for me to see that you know what you're doing. Topics: basic syntax for variables ($scalar, @arrays, %hashes), conditionals (if), loops (while, for), and booleans (and, or, ==, etc), command line input (<>) and output (print). You may use subroutines if you like, but I'm not trying to test that here, nor am I looking for detailed work with regular expressions. I'll be out of town this weekend but back Sunday night; you can email me if you have questions and I'll get back to you then. -- 1 ------------- # Find and correct the syntax errors in the following code. $a = 2 b = "The value of a is " , a; if ($a = 3) { Print "yup, "a" is three."; } -- 2 ------------- # Write a short program which assigns ($P,$Q) to # the four possible boolean combinations, namely # (true,true), (true,false), (false, true), (false,false), # and shows for each that the following two lines are # equivalent. Use appropriate Perl-ish quantities to # represent 'true' and 'false'. not ($P and $Q) (not $P) or (not $Q) # By the way, this is a well known fact in logic # that shows that you can use define the logical # "or" operation in terms of the logical "and" and "not". -- 3 ------------- # Write two programs that print the integers from 1 to 10, # one using a "for" loop, and one using a "while" loop. -- 4 ------------- # Explain what the "push", "pop", "shift", and "unshift" # array operations do. Show an example of each, printing # out the array before and after the operation. # Show how you can do the same operation as "push" by # using an array assignment statement instead. -- 5 ------------- # Write a short program that asks the user to type an integer # from 1 to 5, and then prints out that number as an English # word. A sample dialog would look like this. computer> Type an integer from 1 to 5. user> 4 computer> You typed 'four'. # For full credit do this in two ways, first using "if" and "elsif", # and second using a hash, and also complain if the input is not # integer from 1 to 5. -- 6 ------------- # Figure out what the following program does. Explain. # (I suggest trying it with various versions of the first line.) # Add appropriate comments and replace these variable # names with ones that are clearer. my @x=('jim','joe','john'); my $y=""; for my $z (@x){ if ($y lt $z){ $y=$z; } } print "$y\n";