Introduction to Perl - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Introduction to Perl

Description:

Lists and/or arrays are structures that easily lend themselves to looping. ... get 12 items from a user and print in alphabetical order ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 16
Provided by: test243
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Perl


1
Introduction to Perl
  • Unit 6 Arrays

2
Extended Variables
  • Lists and/or arrays are structures that easily
    lend themselves to looping.
  • An array is denoted by the _at_ symbol, while a hash
    is denoted by a .
  • The different values of the arrays are stored in
    subscripted locations. i.e. array0firstar
    ray1 second

3
More About Arrays
  • Arrays can contain multiple variable types, such
    as scalars, strings, and other arrays.
  • Arrays use square brackets

4
Hashes
  • Hashes (also called associative arrays) work like
    arrays and elements are accessed by key values.
  • Hashes use curly braces

5
Whole vs. Part
  • When using arrays and hash tables, there are 2
    ways of modifying data. Either you can change the
    whole array/hash, or individual elements.
  • To access a specific element, put a sign in
    front of the variable name, and then either the
    index or key value.e.g. _at_array -gt array422
  • hashTable -gt hashTable12tony
  • Also if you wish to store values to a hash or an
    array, you would do so using the dollar sign as
    well.
  • e.g. studentArray3 Mark hashBrownegg
    4

Notice the different types of braces
6
Random Access
  • It is also possible to have "Random Access" of
    the array
  • Note that you have to access an individual
    scalar element with a
  • The first element of the array has the
    subscript of 0
  • print "\nThe first element of the array
    array0\n"
  • print "The second element of the array
    array1\n"
  • The function scalar returns the length of the
    array
  • print "\nThe length of the array is
    ",scalar(_at_array), "\n"

7
Arrays and Loops
  • get 12 items from a user and print in
    alphabetical order
  • This inputs the elements from the user.
  • my _at_array
  • for(i 0 i lt12 i )
  • print Enter item , i 1,
  • arrayi ltSTDINgt
  • _at_array sort(_at_array)
  • print _at_array This prints out the entire
    array.

8
foreach() and Arrays
  • Each of these does the same task.
  • _at_array "mark", "dave", "tony"
  • foreach element (_at_array)
  • print element
  • ------------------------------------
  • The default variable is shorthand.
  • foreach (_at_array)
  • print _
  • ------------------------------------
  • for( my x 0 x lt _at_array x)
  • print arrayx

9
Using an Array
  • my num, count 0
  • my _at_array
  • do
  • print Please enter a number to add (or 999 to
    end)
  • num ltSTDINgt
  • count
  • if(num ! 999)
  • arraycount num
  • while(num ! 999)
  • Get the sum of numbers in an array
  • my sum 0
  • foreach(_at_array)
  • sum _

10
Common Array Functions
  • Two important array functions are split and join.
  • split takes a string and splits it at the
    delimiter (placeholder where next character
    starts) and returns an array.
  • join takes a list, inserts a specified delimiter,
    and returns a string.
  • Common delimiters include space, return, tab, and
    commas.

11
Joining a List
  • A list is a set of elements separated by commas
  • It can contain different scalars or even other
    lists.
  • You can print out a list.
  • print ("Hello ", 3, " World\n")
  • Join joins a list into a string
  • tmp join ("_ _", ("Hello ", 3, " World"))
  • print ("\nJoined list ", tmp, "\n" )

12
Printing and Temp Variables
  • You can print using a temporary variable
  • tmp join ("_ _", ("Hello ", 3, " World"))
  • print "Joined list (no parenthesis) ", tmp,
    "\n"
  • If the data you desire is not needed later, you
    dont
  • need to store it in memory. (i.e. tmp is not
    needed
  • to be declared).
  • print "Joined list (no intermediate variable) ",
    join ("_ _", ("Hello ", 3, " World")), "\n\n"

13
Splitting a String
  • split returns a list from a string broken at
    delimiters
  • my _at_array split('x', "Oncexuponxaxmidnightxdrear
    y,\n")
  • print Split result\n
  • foreach( _at_array )
  • print _, \n
  • The array can also be sorted
  • print \n\nSorted result\n
  • my _at_sort_array sort( _at_array )
  • foreach( _at_sort_array )
  • print _, \n

14
Stack Array Functions
  • Push pushes an element onto the end of an array.
  • Push adds elements to the end of an array
  • Push adds the new element after all of the
    existing elements
  • push _at_array, 13
  • print "Array after push _at_array\n"
  • Pop pops an element off of the end of an array.
  • Removes elements from the end of the array
  • my temp pop(_at_array)
  • print "Array after pop _at_array\t\ttemp
    temp\n\n"

15
More Stack Functions
  • There are similar operators for the front.
  • Unshift adds items to the front (zero subscript)
    of an array.
  • This might not make as much sense as push, as its
    roots are buried in computer terminology.
  • unshift( _at_array, temp )
  • print "Array after unshift _at_array\n"
  • Shift removes an element from the front of an
    array.
  • my temp2 shift( _at_array )
  • print "Array after shift _at_array\t\ttemp2
    temp2\n"

Array Exercises
Write a Comment
User Comments (0)
About PowerShow.com