High Level Languages - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

High Level Languages

Description:

High Level Languages – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 49
Provided by: csNo
Category:

less

Transcript and Presenter's Notes

Title: High Level Languages


1
High Level Languages
  • Perl
  • Lecture 2

Chris Coleman cqc_at_cs.nott.ac.uk
2
Lecture 1 Summary
  • Last time we did
  • Intro to Perl
  • Hello World
  • Running Perl programs
  • Scalars
  • Numerical operations
  • String operations
  • Conditionals

3
Lecture 2 Summary
  • Now onwards to
  • Loops
  • Arrays
  • Hashes

4
Loops
  • Perl supports six kinds of loop
  • while
  • do-while
  • until
  • do-until
  • for
  • foreach
  • The condition part of each loop has the same
    rules as for if statements

5
Loops While
  • A while loop runs the following block of code
    until the condition is no longer true
  • If the condition was never true the code will not
    be executed even once

6
Loops While
  • will print out value from 0 to 9
  • not including 10
  • a 0
  • while (a lt 10)
  • print "a is ",a," \n"
  • a

7
Loops While
  • nothing will get printed here
  • a 5
  • while (a gt 10)
  • print "a is ", a, "\n"
  • a--

8
Loops Do-while
  • A do-while loop has the test at the end of the
    block of code
  • This means the code will get executed at least
    once

9
Loops Do-while
  • this will print out "a is 5"
  • a 5
  • do
  • print "a is ", a, "\n"
  • a--
  • while (a gt 10)

10
Loops Until, Do-until
  • until and do-until behave like while and do-while
    except with the tests reversed
  • print 0 to 9
  • a 0
  • until (a 10)
  • print "a is ",a ,"\n"
  • a

11
Loops For
  • A for loop is like a while loop except the set-up
    command, test condition and iteration command are
    all on the one line
  • The format is
  • for (setup test iteration)
  • code goes here

12
Loops For
  • prints from 0 to 9
  • for (a 0 a lt 10 a)
  • print "a is ", a, "\n"

13
Loops - Backwards
  • Like if statements, Perl will let you write the
    loop after the command
  • print "hello ",a until (a 10)
  • print "world",a while (a lt 10)
  • This makes more sense when you learn about
    sub-routines

14
Loops Foreach
  • foreach loops are special versions of for loops
  • Used to iterate through each entry in an array
  • Arrays? We're getting to that...

15
Loops Controlling
  • In any of these loops you can use the next and
    last commands
  • next means skip the rest of the loop block but
    carry on running the loop (similar to continue in
    Java)
  • last will exit the loop completely (break in Java)

16
Loops Next
  • print even numbers between 1 and 10
  • for (a 1 a lt 10 a)
  • test if this number is odd
  • skip if it is
  • if ((a 2) 1)
  • next
  • print a," is even\n"

17
Loops Last
  • print numbers between 50 and 100
  • abort if number is divisible by 17
  • for (a 50 a lt 100 a)
  • if ((a 17) 0)
  • last
  • print "number ", a, "\n"

18
Loops Labels
  • If you inside a nested loop next and last will
    only affect the inner loop
  • If you want to affect the outer loop you can use
    labels
  • A label goes before the start of the loop and
    after the next or last

19
Loops Labels
  • print out products until
  • result goes over 200
  • OUTER for (a 0 a lt 100 a)
  • for (b 0 b lt 100 b)
  • total a b
  • last OUTER if (total gt 200)
  • print a, "x", b, "", total, "\n"

20
Arrays
  • The concept of arrays is the same as for other
    languages
  • Whole arrays in Perl are identified by the _at_
    symbol
  • No need to declare size of array before you use
    it, arrays will increase or decrease in size as
    they need to
  • First entry is number 0
  • An array can store a mix of data types

21
Arrays
  • Individual entries in the array are scalars, so
    referenced by a with square brackets after,
    e.g.
  • First item is number 0 (not 1 like in Pascal)
  • set the first entry in name array
  • to a string
  • name0 "Duncan" right
  • _at_name0 "Duncan" wrong

22
Lists
  • Lists are not arrays, but it's best to mention
    them now
  • Lists are a collection of data between round
    brackets, separated by a comma
  • You can assign to an array from a list, e.g.
  • _at_names ("Duncan", "Jim", "Saffron")
  • print names0 prints Duncan

23
Lists
  • Lists can contain arrays, so
  • _at_extra ("Saffron", "Liz")
  • _at_names ("Duncan","Jim", _at_extra, "Mark")
  • print names0 Duncan
  • print names1 Jim
  • print names2 Saffron
  • print names3 Liz
  • print names4 Mark

24
Lists
  • To avoid all the quotes and commas you can use
    the qw (quote words) function
  • one way
  • _at_names ("Duncan", "Jim", "Saffron"
  • another way
  • _at_names qw(Duncan Jim Saffron)

25
Arrays Functions
  • push _at_array, list
  • Adds the contents of list to the end of _at_array
  • _at_names qw(Duncan Jim)
  • push _at_names, "Saffron"
  • print names2 Saffron
  • push _at_names, "Mark", "Liz"
  • print names4 Liz

26
Arrays Functions
  • pop _at_array
  • Removes and returns the last element in the array
  • _at_names qw(Duncan Jim Saffron)
  • onename pop _at_names
  • print onename Saffron
  • remove last entry, ignore the value
  • pop _at_names
  • pop and print all in one line
  • print pop _at_names Duncan

27
Arrays Functions
  • unshift _at_array, list
  • Same as push but operates on the start of the
    array
  • shift _at_array
  • Same as pop but operates on the start of the
    array
  • sort _at_array
  • Returns an alphabetically sorted version of the
    array

28
Arrays Length
  • You can get the length of an array by using the
    array in scalar context
  • _at_names qw(Duncan Jim Saffron)
  • length _at_names length is 3

29
Arrays Length
  • You might have to force scalar context with the
    scalar keyword
  • _at_names qw(Duncan Jim Saffron)
  • print _at_names will print the
  • contents of the array
  • print scalar _at_names will print 3

30
Arrays Foreach
  • Remember foreach?
  • You use this to iterate through all the entries
    in an array
  • So, to print out all the contents of an array
  • _at_names qw(Saffron Duncan Jim)
  • foreach item (_at_names)
  • print item,"\n"

31
Arrays Foreach
  • Since sort takes an array and returns an array
    you can use it to sort the array whilst printing
  • _at_names qw(Saffron Duncan Jim)
  • foreach item (sort _at_names)
  • print item,"\n"

32
Arrays Slices
  • A slice is a subset of an array, built up form
    the specified indexes
  • The indexes can be specified individually or by
    using a range
  • _at_names3..5 entries 3 to 5
  • _at_names1, 4, 6 entries 1, 4 and 6
  • _at_names0..5, 9 entries 0 to 5, and 9

33
Arrays Slices
  • You can use slices in a number of ways
  • set _at_little to be the first 100 entries
  • of the array _at_big
  • _at_little _at_big0..99
  • replace entries 4 to 6 in array _at_names
  • with new values
  • _at_names4..6 qw(Kev Sally Sarah)

34
Arrays Slices
  • When using a slice to replace values in an array,
    you don't need the same number of values on the
    right hand side
  • replace 10 names with just 2
  • _at_names5..14 qw(Jon Harry)

35
Arrays Java baiting
  • Consider the amount of code you'd need in Java
    (or another language) to have an array type
    structure that can accept any data type and then
    print them all out.
  • I was going to write the Java code out but I got
    bored, it involved multiple Vector objects
    though...

36
Arrays Java baiting
  • The Perl version
  • push _at_list, "some string", 23, 3.14
  • foreach item (_at_list)
  • print "item ", item, "\n"

37
Hashes
  • Hashes (aka Associate arrays) are very usefull
  • Like an array, but instead of referencing an item
    by number you reference it by a string
  • Whole hashes are identified with a , individual
    values are scalars so use before and after.

38
Hashes Example
  • age'Duncan' 24
  • age'Jim' 28
  • print age'Duncan' prints 24

39
Hashes Assigning to
  • You can assign to a hash from a list or array
  • The keys will be the 1st, 3rd, 5th.. entries
  • The values will be the 2nd, 4th, 6th entries
  • ages ('Duncan',24,'Jim',28)
  • print ages'Jim' prints 28
  • You can use gt as a sugar when assigning
  • ages ('Duncan' gt 24, 'Jim' gt 28)

40
Hashes Functions
  • exists
  • Returns true if a key exists
  • age('Duncan' gt 24, 'Jim' gt 28)
  • unless (exists age'Saffron')
  • print "Age not known\n"
  • delete
  • Deletes a key from the hash
  • delete age'Duncan'

41
Hashes Functions
  • keys
  • Returns a list of all the keys in the hash
  • The keys will be in no particular order
  • ages ('Duncan' gt 24, 'Jim' gt 28)
  • _at_mykeys keys ages
  • print "1st key ", mykeys0, "\n"
  • print "2nd key ", mykeys1, "\n"

42
Hashes Looping
  • To loop through a hash (say to print it) use keys
    to get a list of keys and then a foreach loop.
  • ages ('Duncan' gt 24, 'Jim' gt 28)
  • _at_allnames keys ages
  • foreach name (_at_allnames)
  • print name, " is "
  • print agesname," years old\n"

43
Hashes Looping
  • There's no need to store the result of keys in an
    array, as we can put the expression straight into
    the foreach loop
  • ages ('Duncan' gt 24, 'Jim' gt 28)
  • foreach name (keys ages)
  • print name, " is "
  • print agesname," years old\n"

44
Hashes Sorting
  • Hashes are unordered whilst stored, you can only
    sort them when you come to work with them
  • Sorting the keys into alphabetical order is
    pretty easy
  • Remember the sort function takes an array and
    returns a sorted version of that array
  • So...

45
Hashes Sorting
  • ages ('Duncan' gt 24, 'Jim' gt 28)
  • foreach name (sort keys ages)
  • print name, " is "
  • print agesname," years old\n"

46
Data structures Advanced
  • Hashes and arrays aren't only capable of storing
    scalars, they can store hashes and arrays as well
  • So you can have
  • Arrays of arrays
  • Arrays of hashes
  • Hashes of hashes
  • Hashes of arrays
  • We won't be covering this...

47
Data Structures Warning
  • You're allowed to have different data types with
    the same name
  • camel ("humps" gt 2)
  • _at_camel ("bob", "fred")
  • camel 4
  • print camel'humps' 2
  • print camel1 "fred"
  • print camel 4
  • It's not a good idea though

48
Lecture 2 Summary
  • We just did
  • Loops
  • Arrays
  • Hashes
Write a Comment
User Comments (0)
About PowerShow.com