210 likes | 336 Vues
IT441 Network Services Administration. Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu 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
E N D
IT441Network Services Administration Prof. Alfred J Bird, Ph.D., NBCT abird@cs.umb.edu 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 • 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!
Data Structures • In PERL there are three types of data structures: • Scalars • Arrays • Hashes • Each structure has it own naming syntax. • $scalar • @array • %hash
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.
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!
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 @.
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>;
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?
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.
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.
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 May June July August September October November December#; print (@month[4]);
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.
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]);
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.
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]);
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.
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”);
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
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.
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?????
For next time • Read pages 95 to 114 in the textbook.