IT441 Network Services Administration
-
Upload
shellie-farmer -
Category
Documents
-
view
23 -
download
1
description
Transcript of IT441 Network Services Administration
![Page 1: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/2.jpg)
Comment Blocks
Perl normally treats lines beginning with a # as a comment.
Get in the habit of including comments with your code.
Put a comment block at the beginning of your code which includes your name, the name of the module, date written and the purpose of the code.
![Page 3: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/3.jpg)
Comment Blocks #!/usr/bin/perl -w##Module Name: helloWorld.pl##Written by Alfred J Bird, Ph.D., NBCT#Date Written – 21 September 2011#Purpose: To print out the string “Hello world!”##Date Modified – 25 September, 2011#Purpose of modification: To fix spelling errors.#Modified by: Al Bird#print “Hello world! \n”;
![Page 4: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/4.jpg)
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 5: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/5.jpg)
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 6: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/6.jpg)
Scalars
We talked about scalars in the past. 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 7: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/7.jpg)
Lists I do not consider a list a data structure
but some authors, teachers and CS pros do so be careful .
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 8: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/8.jpg)
Using a List
You have already been using lists without knowing it.
When you type the following statementprint (“Hello ”, “world”, “! “, “\n”);
You are passing a list to the print function. I have just used a new Perl term,
function. A function is a subroutine (a free standing
piece of code) or an operator that returns a value and/or does something
![Page 9: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/9.jpg)
Another Way to Create a List
Given a list we created this way:(‘Hello’, ‘world.’, ‘I’, ‘am’, Al’)
We can use another method to create it:qw/Hello world I am Al/
As with earlier similar operators we can use any nonalphanumeric character as a separator:
qw#Hello world I am Al#qw&Hello world I am Al&qw{Hello world I am Al}
![Page 10: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/10.jpg)
A Third Way to Create a List
We can create a list by using a range. This list (1, 2, 3, 4, 5, 6) Is the same as this list (1..6)
But this will not work: (6..1) does not give (6, 5, 4, 3, 2, 1) because the left hand side must be less than
the rhs To get the list (6, 5, 4, 3, 2, 1) using a
range we need to type reverse (1..6) Try these using a print statement!
![Page 11: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/11.jpg)
Printing a List
Remember that a list is ordered! The elements have a location that can be
counted The counting starts with 0 (the 1st element is
number 0) How do we print a list? What is the result of the following
statements?print (qw/a b c d e f g/);
How about this statement?print qw/a b c d e f g/;
First predict the results and then try them and see what happens.
![Page 12: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/12.jpg)
Printing Individual List Elements
We can refer to individual elements in a list by using a number in brackets [] after the list.
What is the result of the following statement?
print ((qw/a b c d e f g/)[2]); How about this statement:
print ((‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’) [3]); First predict the results and then try them
and see what happens. You can put a scalar variable into the
braces$i = 3;print ((qw/a b c d e f g/)[$i]);
![Page 13: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/13.jpg)
A List Slice
We can refer to a range inside the braces. What do we get when we run the
following statement:print ((qw/a b c d e f g/)[2..4]);
First predict the results and then run the statement.
What about this statement:print ((qw/a b c d e f g/)[3..1]);
![Page 14: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/14.jpg)
Extras
What do you think will happen if you enter the following code:
print ((‘z’, ‘x’, ‘c’, ‘v’, ’b’, ‘n’, ‘m’)[-1]); First make a prediction and then run the
code. How about this code
$i=2.9; print ((‘z’, ‘x’, ‘c’, ‘v’, ’b’, ‘n’, ‘m’)[$i]);
First make a prediction and then run the code.
![Page 15: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/15.jpg)
A Coding Problem.
Write a program that: Creates a list with the names of the months
and use it in a print statement to print out the month you were born.
![Page 16: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/16.jpg)
A Coding Problem.
Write a program that: Creates a list with the names of the months
and use it in a print statement to print out the month you were born.
#!/usr/bin/perl/print (qw#January February March April May
June July August September October November December#[4]);
![Page 17: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/17.jpg)
Another Coding Problem. Write a program that:
Asks you the numeric value of the month you were born, creates a list with the names of the months and use this list in a print statement to print out the month you were born based on the data value you entered.
![Page 18: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/18.jpg)
Another Coding Problem. Write a program that:
Asks you the numeric value of the month you were born, creates a list with the names of the months and use this list 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>);print (qw&January February March April May
June July August September October November December&[$month-1]);
![Page 19: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/19.jpg)
A Third Coding Problem.
Write a program that: Asks you the numeric value of the month you
were born, creates a list with the names of the months and use this list 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 20: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/20.jpg)
A Third Coding Problem. Write a program that:
Asks you the numeric value of the month you were born, creates a list with the names of the months and use this list 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>);print (qw&January February March April May
June July August September October November December&[$month-2..$month]);
![Page 21: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/21.jpg)
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 a @.
![Page 22: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/22.jpg)
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 23: IT441 Network Services Administration](https://reader035.fdocuments.in/reader035/viewer/2022062721/56813650550346895d9dd220/html5/thumbnails/23.jpg)
For next time
Read pages 81 to 99 in the textbook.