CSC 4630 - PowerPoint PPT Presentation

About This Presentation
Title:

CSC 4630

Description:

CSC 4630 Perl 2 adapted from R. E. Beck I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the user for a ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 17
Provided by: support
Category:
Tags: csc | sort

less

Transcript and Presenter's Notes

Title: CSC 4630


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, ab, a-b)

6
List Literals (2)
  • The empty list, denoted by ()
  • Values generated by the list constructor operator
    denoted 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 _at_ (rather than ).
  • Can be treated as a whole by certain array
    operators and array functions. (Similar to LISP
    or APL)

8
Array Operators
  • Assignment
  • _at_a (8, _at_a) Appends 8 as the first element of
    _at_a.
  • _at_fred qw(one two)
  • _at_barney (4, 5, _at_fred, 8, 9) Makes _at_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, _at_fred) (a, b, c)
  • d becomes the value of a
  • _at_fred becomes (b, c)
  • Array sharing, array decomposition
  • (e, _at_fred) _at_fred
  • e becomes the first element of _at_fred
  • _at_fred becomes tail(_at_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 _at_fred sets a to length(_at_fred)
  • Compare with
  • (a) _at_fred
  • _at_a (a)

11
Array Element Access
  • Index origin is 0
  • Example
  • _at_fred (1..5)
  • fred2 is 3
  • Slice a subarray chosen by an index list
  • Example
  • _at_fred (1..5)
  • _at_fred2,3,4 is (3,4,5)

12
Array Element List (2)
  • More slices
  • _at_fred0,1,2 _at_fred1,1,1 makes the first
    three elements of _at_fred equal to fred1
  • _at_fred (7,8,9)
  • _at_barney (2,1,0)
  • _at_bf _at_fred_at_barney so _at_bf is (9,8,7)

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

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

15
Arrays as Stacks (2)
  • a shift(_at_fred) assigns the first element of
    _at_fred to a and removes it from _at_fred

16
More Array Functions
  • reverse
  • sort
  • chomp
Write a Comment
User Comments (0)
About PowerShow.com