Perl - PowerPoint PPT Presentation

About This Presentation
Title:

Perl

Description:

Creates list from each word with single quotes. qq(the ... Lists occur in code but arrays variables are needed to store and ... snum = sort {$a = $b; ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 15
Provided by: royl2
Learn more at: https://www.cse.fau.edu
Category:
Tags: perl | snum

less

Transcript and Presenter's Notes

Title: Perl


1
Perl
  • Lists and Arrays

2
Lists
  • List is a sequence of scalars
  • Surrounded by parentheses items separated by
    commas
  • (a, 5, alfa, _, the item)
  • Usual variable substitution rules apply

3
List Shortcuts
  • qw(the speed color fox)
  • Creates list from each word with single quotes
  • qq(the speed color fox)
  • Creates list from each word with double quotes
  • (1 .. 9, 11..15)
  • Range of numbers

4
Arrays
  • Lists occur in code but arrays variables are
    needed to store and manipulate list values
  • _at_names qw(Joe, Jill, Mary, Bill)
  • _at_empty ()
  • _at_double (_at_names, _at_names)
  • Array is flattened to list

5
Arrays (cont.)
  • _at_nums (1..10)
  • (first, _at_rest, last) _at_nums
  • first 1 _at_rest (2..10) last undef
  • Arrays are greedy
  • (f,n) _at_nums
  • f1 n2 ignore rest
  • (a, b, c) qq(a b)
  • aa bb c undef

6
Array Subscripts
  • Zero-based subscripts
  • _at_nums (1..10)
  • nums0
  • gives or takes the first element
  • _at_nums1,2,5,7,9 is array slice
  • nums-1
  • Last element -2 is next to last, etc.

7
Array Size
  • array
  • Returns highest subscript of array
  • Setting it changes size of array
  • size _at_array
  • Gives size of array

8
Context
  • Determines effect of operations
  • Scalar context
  • Operations use scalars
  • List context
  • Operations use lists (or arrays)
  • In assignment, left side determines context

9
Context (cont.)
  • ab scalar context
  • _at_m_at_n list context
  • b_at_list scalar context
  • _at_ab list context, one element list
  • lineltSTDINgt scalar so one line
  • _at_inputltSTDINgt list so whole file
  • (x)ltSTDINgt list, but what?

10
Context (cont.)
  • Context can have significant effects
  • _at_start() x 100 array size 100 of
  • which(a, b, c) scalar comma expr
  • localtime function
  • Scalar formatted date string
  • List array of date/time components

11
Stepping through an array
  • foreach i (1..10)
  • foreach (_at_name)
  • Uses _
  • Modifying the control variable alters the
    corresponding entry in the array

12
Split and join
  • split(/ /, words)
  • Splits string words on blanks
  • First argument is a pattern, a regular expression
    to match between items
  • //, the empty string splits each letter
  • join(, , (1..5))
  • Inserts comma space between numbers

13
Sorting
  • _at_alist sort _at_names
  • Alphabetic sort
  • _at_snum sort a ltgt b (5, 3, 7, -1)
  • Can specify any code block or function to
    determine sort order. Must return -1, 0, or 1 to
    indicate order

14
Arrays as Stacks
  • Operations on arrays
  • push array, new_list on back
  • pop array returns last entry
  • shift array, new_list on front
  • unshift array returns first element
  • How can you make a queue?
Write a Comment
User Comments (0)
About PowerShow.com