Arrays - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Arrays

Description:

Arrays – PowerPoint PPT presentation

Number of Views:310
Avg rating:3.0/5.0
Slides: 26
Provided by: acmCseB
Category:
Tags: arrays | cat | kittens

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • Lists, Arrays, Operators
  • Array Assignment
  • push, pop, shift, unshift
  • Array Interpolation
  • Array Element Access
  • Slices
  • Negative subscripts

2
Lists, Arrays, Operators
  • List, an ordered set of scalars
  • Array, a variable that holds a list
  • Each element of an array is a scalar
  • Smallest array has zero (0) elements
  • Largest only limited by virtual memory
  • Perl philosophy no unnecessary limits

3
List Literals
  • (1, 2, 5, 10)
  • ("horse", "pig", "chicken")
  • ("x", 3, 5, 4, "z")
  • (a, 17)
  • (ab, cd ) how many elements here?
  • () the empty list, zero elements
  • (1..5) the list constructor function '..'
  • ('a'..'z') works for letters too!
  • (a..b)

4
Arrays (_at_) Hold Lists
  • _at_array '_at_' character indicates List
  • a not related to _at_a
  • a0 is the first element of _at_a, a1 is the
    second, etc

5
Initializing an Array
  • literal array assignment
  • _at_a ('apple', "cat", "dog", 'emu')
  • or use the quote words (qw) function
  • _at_a qw(happy birth day)
  • _at_b qw/chicken fox goat/

6
Array Assignment ()
  • _at_fred (1,2,3)
  • _at_barney _at_fred
  • _at_one 1 makes _at_one the list (1)
  • _at_fred qw(one two)
  • _at_bambam (4, 5, _at_fred, 6)
  • _at_bambam is (4, 5, "one","two", 6)
  • _at_bigger ( _at_bigger, 'one more' )
  • adds one element to end of _at_bigger
  • _at_bigger ( 9, _at_bigger )
  • adds 9 to the beginning of the
  • _at_bigger array

7
Array/List on the Left
  • (a, b, c) (4, 5, 6)
  • assigns three variables
  • (x, y) (y, x) Swap!
  • (d, _at_fred) (a, b, c)
  • d is a, _at_fred is (b,c)
  • (d, _at_fred, noval) (a, b, c)
  • noval gets undef -- why?
  • (a, b) (9) b gets undef -- why?

8
Array Operations
  • _at_rare _at_common (9,8,7) both arrays get
    assigned
  • length scalar _at_common
  • yields the length of the array
  • (number of elements).
  • This is SCALAR context.
  • length _at_common
  • same thing as above 'scalar' assumed

9
Array Element Access
  • _at_ages (7, 8, 9)
  • first ages0 first gets 7
  • ages0 5 _at_ages now is (5,8,9)
  • second ages1 second gets 8
  • ages2 makes the array (5,8,10)
  • ages1 4 makes the array (5,12,10)

10
More Array Element Access
  • swap first two elements of _at_f
  • (f0, f1) (f1, f0)

11
Slices
  • Slices let you access several elements of an
    array at once. They are a more advanced topic
    and less frequently used than some other
    functions, but important to be familiar with.
  • _at_names qw(lwall joelg gnat)
  • _at_small_list _at_names1,2
  • _at_small_list is now ('joelg','gnat')

12
More slices
  • So, _at_f0,1 is the same as (f0, f1)
  • _at_f0,1 _at_f1,0 swap first two elements
  • _at_f0,1,2 _at_f1,1,1
  • set all the first three elements to the
  • value of 2nd element (weird but legal)
  • _at_f1,2 (9,10) replace element 1 and 2

13
More slices
  • Work on literal lists
  • _at_who (qw(ape bear cat dog))2,3
  • what is _at_who?
  • Also work on any function that returns a list
  • here's a made up function called
  • 'get_pets' that returns a list of scalars
  • my (pet1, pet2) (get_pets ('age',5))3,4
  • Any function that returns a list is eligible for
    slicing

14
Array Index Values
  • _at_numbers (7,8,9)
  • a 2
  • b numbersa b gets 9
  • c numbersa-1 c gets 8
  • (d) (7,8,9)a-1 d gets 8
  • For slices
  • _at_b (2,1,0)
  • _at_numbers_at_b is (9,8,7)

15
undef
  • _at_numbers (7,8,9)
  • b numbers7 b is undef
  • numbers3 'hi'
  • numbers6 'world'
  • _at_numbers is now
  • (7, 8, 9, 'hi', undef, undef, 'world')

16
array
  • array is the index of the last element in an
    array.
  • _at_numbers qw(1 2 3 4 5 6)
  • print "numbers\n" prints 5
  • an empty array has array -1

17
Negative Subscripts
  • _at_numbers (7,8,9)
  • print "numbers-1\n"
  • prints 9 (count -1 back from end of array)
  • print numbers-2 prints 8
  • numbers is 2
  • print "numbersnumbers\n" prints 9
  • arrayarray is a common idiom for the last
    element

18
shift, unshift, push, pop
  • (blue, red, purple, green)

19
shift, unshift
  • Both operate on the front (or top, depending on
    how you think of it) of the array.
  • _at_list qw(blue red purple green)
  • first shift _at_list
  • first gets "blue", and _at_list becomes
  • (red, purple, green)
  • unshift (_at_list, "orange")
  • _at_list becomes (orange, red, purple, green)

20
pop, push
  • Both operate on the back (or bottom, depending on
    how you think of it) of the array
  • _at_list qw(orange red purple green)
  • last pop _at_list
  • last gets 'green', and list becomes
  • (orange, red, purple)
  • push( _at_list, 'blue')
  • _at_list is (orange, red, purple, blue)

21
Multiple push, unshift
  • Can push or unshift more than 1 element
  • push _at_animals, qw/dog cat/
  • _at_cats qw(whiskers fluffy)
  • _at_kittens ('snowball', 'susie')
  • unshift _at_cats, _at_kittens make cats bigger
  • push _at_numbers, (1..20)
  • push _at_list, _at_list2,3 does what?

22
reverse
  • _at_animals qw(elk antelope ant)
  • _at_reversed reverse _at_animals
  • _at_reversed is (ant antelope elk)
  • _at_animals HAS NOT CHANGED
  • _at_animals reverse _at_animals changes _at_animals
    itself

23
sort for an Array
  • _at_x qw(scissors paper rock)
  • _at_sorted sort _at_x
  • _at_sorted is (paper, rock, scissors)
  • The sort is in ASCII order that is,
    alphabetical.
  • It can be done other ways, like numerical (to be
    covered later).
  • Sort would do this to numbers
  • _at_nums sort (1,2,4,20,16)
  • yields (1,16,2,20,4)

24
chomp for an Array
  • _at_stuff ("hello\n", "my\n", "friends\n")
  • chomp _at_stuff drops "\n" from each element
  • This might happen if you read a file into
  • an array variable. Each line of the file
  • would have a \n

25
print for an Array
  • _at_animals qw(dog cat goat)
  • print _at_animals
  • runs elements together -- "dogcatgoat"
  • print "_at_animals"
  • separates elements with space chars --
  • "dog cat goat"
  • You can also use the join operator or a
  • for/foreach loop, to be covered soon
Write a Comment
User Comments (0)
About PowerShow.com