BuiltIn Functions - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

BuiltIn Functions

Description:

BuiltIn Functions – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 20
Provided by: PaulL155
Category:
Tags: builtin | functions | gid

less

Transcript and Presenter's Notes

Title: BuiltIn Functions


1
Built-In Functions
2
Notations
  • For each function, I will give its name and
    prototype.
  • prototype number and type of arguments
  • ARRAY means an actual named array (i.e., variable
    starting with _at_)
  • LIST means any list of elements (i.e., a list
    literal or a named array)
  • HASH means a named hash variable ()
  • other types will identify the purpose of a scalar
    value

3
push ARRAY, LIST
  • add values of LIST to end of ARRAY
  • push _at_array, 5
  • adds 5 to end of _at_array
  • push _at_foo, (4, 3, 2)
  • adds 4, 3, and 2, to the end of _at_foo
  • _at_a (1, 2, 3) _at_b (10, 11, 12)
  • push _at_a, _at_b
  • _at_a now ? (1, 2, 3, 10, 11, 12)

4
pop ARRAY
  • remove and return last element of ARRAY
  • _at_array (1, 5, 10, 20)
  • last pop _at_array
  • last ? 20
  • _at_array ? (1, 5, 10)
  • _at_empty ()
  • value pop _at_empty
  • value ? undef.

5
unshift ARRAY, LIST
  • Add elements of LIST to front of ARRAY
  • unshift _at_array, 5
  • adds 5 to front of _at_array
  • unshift _at_foo, (4, 3, 2)
  • adds 4, 3, and 2, to the front of _at_foo
  • _at_a (1, 2, 3) _at_b (10, 11, 12)
  • unshift _at_a, _at_b
  • _at_a now ? (10, 11, 12, 1, 2, 3)

6
shift ARRAY
  • remove and return first element of ARRAY
  • _at_array (1, 5, 10, 20)
  • first shift _at_array
  • first ? 1
  • _at_array ? (5, 10, 20)
  • _at_empty ()
  • value shift _at_empty
  • value ? undef

7
splice ARRAY, OFFSET, LENGTH, LIST
  • functionality of push, pop, shift, unshift
  • (plus a little bit more)
  • remove LENGTH elements from ARRAY, starting at
    position OFFSET, and replace them with LIST.
  • In scalar context, return last element removed
  • In list context, return all elements removed
  • _at_foo (1 .. 10)
  • _at_a splice _at_foo, 4, 3, a .. e
  • _at_foo ? (1, 2, 3, 4, a, b, c, d, e, 8,
    9, 10)
  • _at_a ? (5, 6, 7)

8
splice w/o some arguments
  • splice ARRAY, OFFSET, LENGTH, LIST
  • Omit LIST remove elements, dont replace
  • Omit LIST and LENGTH remove all elements
    starting at OFFSET
  • Omit LIST, LENGTH, and OFFSET clear entire ARRAY
    as its being read

9
splice equivalencies
  • splice ARRAY, OFFSET, LENGTH, LIST
  • push _at_a, (x, y)
  • splice (_at_a, _at_a, 0, x, y)
  • pop _at_a
  • splice (_at_a, a) remove last element
  • splice (_at_a, -1)
  • shift _at_a
  • splice (_at_a, 0, 1) remove first element
  • unshift _at_a, (x, y)
  • splice (_at_a, 0, 0, x, y)
  • ax y
  • splice (_at_a, x, 1, y) insert at position x

10
keys HASH values HASH
  • keys ? return list of all keys from HASH
  • seemingly random order
  • values ? return list of all values from HASH
  • same random order as keys produces
  • Example hash
  • months (Jan gt January, Feb gt
    February, Mar gt March, )
  • keys (months) ? (Jan, Feb, Mar, )
  • values (months) ? (January, February,
    March, )
  • NOT necessarily in that order.

11
length EXPR
  • return number of characters in EXPR
  • a Hello\n
  • b length a
  • b ? 6
  • Cannot use to find size of array or hash

12
index STR, SUBSTR, OFFSET
  • Look for first occurrence of SUBSTR within STR
    (starting at OFFSET)
  • OFFSET defaults to 0 if omitted
  • Return first position within STR that SUBSTR is
    found.
  • a index Hello World\n, o
  • b index Hello World\n, o, a1
  • a ? 4, b ? 7
  • Returns -1 if SUBSTR not found.
  • rindex ? return last position found

13
reverse LIST
  • in list context, return an array consisting of
    elements in LIST, in opposite order
  • _at_foo (1 .. 10)
  • _at_bar reverse _at_foo
  • _at_foo ? (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • _at_bar ? (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
  • in scalar context, take LIST, concatenate all
    elements into a string, and return reverse of
    that string
  • rev reverse _at_foo
  • rev ? 01987654321

14
stat FILEHANDLE
  • Return a 13-element list containing statistics
    about file named by FILEHANDLE
  • can also be used on a string containing a file
    name.
  • (dev, ino, mode, nlink, uid, gid, rdev,
    size, atime, mtime, ctime, blksize, blocks)
    stat filename
  • See Camel page 801 for full description
  • Common uses
  • _at_info stat file
  • print File size is info7, last access time is
    info8, last modified time is info9\n

15
sort LIST
  • returns LIST sorted in ASCIIbetical order.
  • undef comes before , then sort by ASCII chart
  • does not affect LIST that is passed in
  • note that by ASCII chart, 100 comes before 99
  • _at_f (Banana, Apple, Carrot)
  • _at_sorted sort _at_f
  • _at_sorted ? (Apple, Banana, Carrot)
  • _at_f unmodified
  • _at_nums (97 .. 102)
  • _at_s_nums sort _at_nums
  • _at_s_nums (100, 101, 102, 97, 98, 99)

16
Advanced Sorting
  • You can tell sort how you want a function sorted
  • Write a small function describing the sort order
  • In this function, Perl will assign a and b to
    be two list elements.
  • If you want a to come before b in sort order,
    return 1. If you want b first, return 1.
  • if order of a and b doesnt matter, return 0
  • sub numeric_sort
  • if (a lt b) return 1
  • elsif (a gt b) return 1
  • else return 0

17
Using Your Own Sort
  • Now that we have that function, use it in sort
  • _at_nums (4, 2, 9, 10, 14, 11)
  • _at_sorted sort numeric_sort _at_nums
  • _at_sorted ? (2, 4, 9, 10, 11, 14)
  • Look at that function again
  • if (a lt b) return 1
  • elsif (a gt b) return 1
  • else return 0
  • This can be simplified quite a bit.
  • return (a ltgt b)

18
Simplifying Further
  • We now have
  • sub by_number
  • return (a ltgt b)
  • When sort function is that simple, dont even
    need to declare it
  • _at_sorted sort a ltgt b _at_nums
  • Excellent description of sorting in Llama chapter
    15

19
Exercises for the Reader
  • How might you sort in reverse numeric order?
    i.e. (102, 101, 100, 99, 98, 97)
  • without using the reverse function
  • How would you sort in reverse ASCIIbetical order?
    ie (dog, cat, bear)
  • without using the reverse function
  • How would you get an array consisting of keys of
    a hash table sorted by key?
  • How would you get an array consisting of keys of
    a hash table sorted by value?
Write a Comment
User Comments (0)
About PowerShow.com