prof. alfred j bird, ph.d., nbct [email protected] office – mccormick 3rd floor 607 office hours...

11
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:00 PM to 5:15PM

Upload: barbra-wilcox

Post on 24-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

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:00 PM to

5:15PM

Page 2: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

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 3: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

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);$numDaysCorrect = $numDays[$month-1];if ($month==2 && $year%4==0){$numDaysCorrect=29;}print (“$months[$month-1] $year has $numDaysCorrect days \

n”);

Page 4: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

Coding Problem

Write code using the previous program that will print out a series of statements like the following: The month of January has 31 days …

Assume you do not know how many entries there are in the arrays but that both arrays have the same numbers of entries

Page 5: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

Array Functions

How do we add data to an array? @array = (@array, $scalar); #is one way!

But there is a better 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 6: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

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 7: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

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 an array of number between 0 and 100 (at least 20 numbers). Predict the output before you try it. What happened????? Why?????

Page 8: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

The Overall World of PERL What is a namespace? What is a package?

Packages are perl files with .pm extn and are considered a separate namespace. So a package is nothing but group of related scalars,arrays,hashes and subroutines for a specific purpose. Once a package is included in a .pl file (using "use") and you want to call one of the subroutines of the package, you may have to use the scope resolution operator &package::subroutine1

Page 9: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

The Overall World of PERL

What is a module? Modules are packages which have the

capabilities of exporting selective subroutines/scalars/arrays/hashes of the package to the namespace of the main package itself. So for the interpreter these look as though the subroutines are part of the main package itself and so there is no need to use the scope resolution operator while calling them.

Page 10: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

CPAN

Why use PERL?\ What other languages could we use?

Ruby, Python, Scripting….. TIMTOWTDI!! Other people have already done it! http://www.perl.org http://www.cpan.org http://www.perlmonks.org

Page 11: Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu  Office – McCormick 3rd floor 607 Office Hours – Tuesday and

For next time

Read pages 95 to 114 in the textbook.