SlideServe
Menu
Préférences
© SlideServe · v2026

Perl Programming Exercises: Volume Calculation and String Manipulation

baris avatar
16 diapositives · 305 Views · Jul 15, 2014

This resource provides a series of Perl programming exercises for beginners. The first exercise requires writing a program to calculate the volume of a sphere based on user input of its radius, utilizing the mathematical constant π to 10 significant figures. The second exercise prompts the user for a string and a positive integer, outputting the string repeated the specified number of times. The final exercise modifies the previous program to display each repetition on a new line. Learn about arrays in Perl, including their creation, manipulation, and the use of various array functions.

Perl Programming Exercises: Volume Calculation and String Manipulation - slide 1
01 / 16Perl Programming Exercises: Volume Calculation and String Manipulation
1 / 17

Télécharger la présentation

Perl Programming Exercises: Volume Calculation and String Manipulation
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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