1 / 16

CSC 4630

CSC 4630. Perl 2 adapted from R. E. Beck. I/O and Arithmetic. Form pairs of programmer/investigator/discover

baris
Télécharger la présentation

CSC 4630

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 4630 Perl 2 adapted from R. E. Beck

  2. I/O and Arithmetic • Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the user for a number, representing the radius of a sphere. The program prints out the volume of the sphere. (Note:B to 10 significant figures is 3.141592654)

  3. Operator x Exercise 2. Write a program that prompts the user for a (short) string and a positive integer and outputs the string repeated the number of times indicated. For example, with input ha 4 the program gives hahahaha Exercise 3. Modify the previous program to print the output on separate lines, one for each instance of the input string.

  4. Arrays in PERL • Look much like lists from LISP except elements must be scalars. • Size does not need to be specified initially. • Use length as a descriptive measure. • Values can be specified by list literals.

  5. List Literals Several forms: • Values separated by commas enclosed in parentheses. • (3.4, 5.6, 8.1, 0) • (“fred”, “george”, 1, 3) • Values can be determined by expressions, which are evaluated when the literal is used. • ($a, $b, $a+$b, $a-$b)

  6. List Literals (2) • The empty list, denoted by () • Values generated by the list constructor operatordenoted by .. • (1 .. 5) generates the list (1, 2, 3, 4, 5) • (2.2 .. 5.7) generates the list (2.2, 3.2, 4.2, 5.2) • (0 .. 4, 8, 9) generates the list (0, 1, 2, 3, 4, 8, 9) • Values generated by the quote word function • qw(alice bob chuck) generates the list (“alice”, “bob”, “chuck”)

  7. Array Variable • Holds a single list value, meaning zero or more scalar values. • Name starts with @ (rather than $). • Can be treated as a whole by certain array operators and array functions. (Similar to LISP or APL)

  8. Array Operators • Assignment • @a = (8, @a) # Appends 8 as the first element of @a. • @fred = qw(one two) • @barney = (4, 5, @fred, 8, 9) # Makes @barney have the value (4, 5, “one”, “two”, 8, 9) • ($a, $b, $c) = (1, 2, 3) # Simultaneous scalar assignment • ($a, $b) = ($b, $a) # Swap values (temporary location is hidden)

  9. Array Operators (2) Fancier assignment statements • Embedded array ($d, @fred) = ($a, $b, $c) $d becomes the value of $a @fred becomes ($b, $c) • Array sharing, array decomposition ($e, @fred) = @fred $e becomes the first element of @fred @fred becomes tail(@fred)

  10. Array Operators (3) • Length mismatches • Too many on right: excess discarded • Too many on left: excess set to undef • Length function: implicit $a = @fred # sets $a to length(@fred) • Compare with ($a) = @fred @a = ($a)

  11. Array Element Access • Index origin is 0 • Example: @fred = (1..5) $fred[2] is 3 • Slice: a subarray chosen by an index list • Example: @fred = (1..5) @fred[2,3,4] is (3,4,5)

  12. Array Element List (2) • More slices • @fred[0,1,2] = @fred[1,1,1] makes the first three elements of @fred equal to $fred[1] • @fred = (7,8,9) • @barney = (2,1,0) • @bf = @fred[@barney] so @bf is (9,8,7)

  13. Array Element List (3) • $#fred is the index value of the last element of @fred • So, $fred[$#fred] is the value of the last element of @fred • And so is $fred[-1] since negative indices count backwards from the end, starting at -1.

  14. Arrays as Stacks • push (@fred, “new”) is equivalent to @fred = (@fred, “new”) • $a = pop(@fred) removes the last value of @fred and assigns it to $a. Right is no better than left • unshift (@fred, “new”) is equivalent to @fred = (“new”, @fred)

  15. Arrays as Stacks (2) • $a = shift(@fred) assigns the first element of @fred to $a and removes it from @fred

  16. More Array Functions • reverse • sort • chomp

More Related