Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT441 Network Services Administration

Similar presentations


Presentation on theme: "IT441 Network Services Administration"— Presentation transcript:

1 IT441 Network Services Administration
Prof. Alfred J Bird, Ph.D., NBCT Office – Wheatly 2nd floor Office Hours – MW 3:00PM to 4:00PM

2 Data Types Remember there are three basic data types in Perl
Numeric String Boolean (Logical) I differentiate between data types and data structures. Not every author or teacher does. Some books use the terms interchangeably so watch out!

3 Data Structures In PERL there are three types of data structures:
Scalars Arrays Hashes Each structure has it own naming syntax. $scalar @array %hash

4 Scalars We talked about scalars two weeks ago.
Scalars are a data type that contain one element. It can be a number such as 1 It can be a string such as “Hello World! \n” It can be a boolean such as true or false It can be stored in a variable with a name such as $i. It is the most primitive of the data structures.

5 Lists We talked about lists last meeting.
A list is defined as an ordered set of scalar values. Lists are delimited by parentheses such as () (1) (“a”) (1, 2, 3, 4, 5) (“a”, “b”, “c”, “d”, “e”) (‘e’, ‘d’, ‘c’, ‘b’, ‘a’) Remember that a list is ordered!

6 Another Data Structure
The problem with a list is that it cannot be named! We need to retype the list every time we want to use it. To solve this difficulty we have a data structure called an array. We can give an array a name that starts with

7 Arrays An array is a data structure that has the characteristics of a list but can be named! To store a scalar into a variable we use an assignment statement $a = 1; To store a list into an array we do the same thing: @a = (1,2,3,4,5); @l = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’); @m = qw<az x c v b n m>;

8 Accessing Individual Elements
How do we access an individual element in an array? Just like we did in a list. Using a list if we code: print ((‘now’, ‘is’, ‘the’, ‘time’)[2]); It will print out the Likewise if we define an array: @s = (‘now’, ‘is’, ‘the’, ‘time’); Will also print out the What about print $s[2];? What will it print out?

9 Scalar vs. List Context Why does the statement print $s[2]; work?
Use the prefix for what you want not what you have. This is referred to as list vs. scalar context. It can become a very important concept later.

10 A Coding Problem. Write a program that:
Creates an array with the names of the months and use it in a print statement to print out the month you were born.

11 A Coding Problem. Write a program that:
Creates an array with the names of the months and use it in a print statement to print out the month you were born. #!/usr/bin/perl/ @ month = qw#January February March April May June July August September October November December#; print

12 Another Coding Problem.
Write a program that: Asks you the numeric value of the month you were born, creates an array with the names of the months and use this array in a print statement to print out the month you were born based on the data value you entered.

13 Another Coding Problem.
Write a program that: Asks you the numeric value of the month you were born, creates an array with the names of the months and use this array in a print statement to print out the month you were born based on the data value you entered. #!/usr/bin/perl/ print “Enter the number of the month you were born: “; chomp ($month = <STDIN>); @month = qw&January February March April May June July August September October November December&;

14 A Third Coding Problem. Write a program that:
Asks you the numeric value of the month you were born, creates an array with the names of the months and use this array in a print statement to print out the month before you were born, the month you were born and the month after you were born based on the data value you entered.

15 A Third Coding Problem. Write a program that:
Asks you the numeric value of the month you were born, creates an array with the names of the months and use this array in a print statement to print out the month before you were born, the month you were born and the month after you were born based on the data value you entered. #!/usr/bin/perl/ print “Enter the number of the month you were born: “; chomp ($month = <STDIN>); @months = qw&January February March April May June July August September October November December&; print

16 Another Coding Problem
Write a program that: Asks you the numeric value of the month and the year you were born, creates an array with the names of the months and an array that contains the number of days in a month (remember leap years) and use these arrays in a print statement to print out the month, the year and how many days are in the month you were born based on the info you entered.

17 Write a program that: Asks you the numeric value of the month and the year you were born, creates an array with the names of the months and an array that contains the number of days in a month (remember leap years) and use these arrays in a print statement to print out the month, the year and how many days are in the month you were born based on the info you entered. #!/usr/bin/perl/ print “Enter the number of the month you were born: “; chomp ($month = <STDIN>); print “Enter year you were born: “; chomp ($year = <STDIN>) @months = qw&January February March April May June July August September October November December&; @numDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $numDays if ($month==2 && $year%4==0){$numDays=29;} print “, $year has ”, $numDays, “ days \n”);

18 Array Functions How do we add data to an array?
@array = $scalar); #is one way! But there is a better way!! $scalar; #will do the same thing! push will append the value in $scalar to the top Likewise pop will take the last value in an array and do something with it. $scalar =

19 Array Functions push() and pop() act on the top of an array (the highest indexed end) shift() and unshift() act on the bottom of an array and perform the same function. We already know what reverse() does.

20 Array Functions Another function is sort(). What do you think it does?
Write a simple program to try it with your array of months. Predict the output before you try it. What happened? Now write a simple program to try it with your array of number of days. What happened????? Why?????

21 For next time Read pages 95 to 114 in the textbook.


Download ppt "IT441 Network Services Administration"

Similar presentations


Ads by Google