+ All Categories
Home > Documents > IT441 Network Services Administration

IT441 Network Services Administration

Date post: 22-Feb-2016
Category:
Upload: atira
View: 50 times
Download: 0 times
Share this document with a friend
Description:
IT441 Network Services Administration. Prof. Alfred J Bird, Ph.D., NBCT [email protected] http:// it441-s14-bird.wikispaces.umb.edu / Office – McCormick 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM. Data Types. Remember there are three basic data types in Perl Numeric - PowerPoint PPT Presentation
Popular Tags:
21
IT441 NETWORK SERVICES ADMINISTRATION Prof. Alfred J Bird, Ph.D., NBCT [email protected] http://it441-s14-bird.wikispaces.umb.edu/ Office – McCormick 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM
Transcript
Page 1: IT441 Network Services Administration

IT441

NETWORK SERVICES ADMINISTRATION

Prof. Alfred J Bird, Ph.D., [email protected]

http://it441-s14-bird.wikispaces.umb.edu/

Office – McCormick 3rd floor 607Office Hours – Tuesday and Thursday 4:00PM to

5:15PM

Page 2: IT441 Network Services Administration

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!

Page 3: IT441 Network Services Administration

Data Structures In PERL there are three types of data

structures: Scalars Arrays Hashes

Each structure has it own naming syntax. $scalar @array %hash

Page 4: IT441 Network Services Administration

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.

Page 5: IT441 Network Services Administration

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!

Page 6: IT441 Network Services Administration

Another Data StructureThe 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 a @.

Page 7: IT441 Network Services Administration

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>;

Page 8: IT441 Network Services Administration

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’);print @s[2];

Will also print out the What about print $s[2];? What will it print out?

Page 9: IT441 Network Services Administration

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.

Page 10: IT441 Network Services Administration

A Coding Problem. Write a program that:

Creates an array called @months with the names of the months and use it in a print statement to print out the month you were born.

Page 11: IT441 Network Services Administration

A Coding Problem. Write a program that:

Creates an array called @months 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

MayJune July August September October November December#;

print (@month[4]);

Page 12: IT441 Network Services Administration

Another Coding Problem. Write a program that:

Asks you the numeric value of the month you were born, creates an array called @months 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.

Page 13: IT441 Network Services Administration

Another Coding Problem. Write a program that:

Asks you the numeric value of the month you were born, creates an array called @months 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 ($day = <STDIN>);@month = qw&January February March April May

June July August September October November December&;

print @month[$day-1]);

Page 14: IT441 Network Services Administration

A Third Coding Problem. Write a program that:

Asks you the numeric value of the month you were born, creates an array called @months 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.

Page 15: IT441 Network Services Administration

A Third Coding Problem. Write a program that:

Asks you the numeric value of the month you were born, creates an array called @months 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 ($day = <STDIN>);If ($day == 12) {$day = 0;}@months = qw&January February March April May

June July August September October November December&;

print (@month[$day-2..$day]);

Page 16: IT441 Network Services Administration

Another Coding Problem Write a program that:

Asks you the numeric value of the month and the year you were born, creates an array called @months with the names of the months and an array called @numDays 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.

Page 17: IT441 Network Services Administration

Write a program that:Asks you the numeric value of the month and the year you were

born, creates an array called @months with the names of the months and an array called @numDays 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 = @numDays[$month-1];if ($month==2 && $year%4==0){$numDays=29;}print (@month[$month-1], “, $year has ”, $numDays, “ days \

n”);

Page 18: IT441 Network Services Administration

Array Functions How do we add data to an array?

@array = (@array, $scalar); #is one way! But there is another way!!

push @array, $scalar; #will do the same thing!

push will append the value in $scalar to the top of @array

Likewise pop will take the last value in an array and do something with it. $scalar = pop @array

Page 19: IT441 Network Services Administration

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.

Page 20: IT441 Network Services Administration

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. Predict the output before you try it. What happened????? Why?????

Page 21: IT441 Network Services Administration

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


Recommended