split
We can use the null pattern //
to split a scalar into
an array in which each element represents a character of the scalar.
Therefore, the expression:
split //, "hello"
will return an array containing the elements:
('h', 'e', 'l', 'l', 'o')
localtime
function
The localtime
function will determine the current local time.
It returns an array consisting of the various attributes related to
the current time including the time and date, the current week day,
day of the year and whether or not daylight savings time is in effect.
(See S&P p.164 for details.)
We can also provide localtime
with a timestamp and it
will return the appropriate time/date information for that timestamp.
A timestamp is simply the number of seconds that have elapsed
since the so-called epoch, which, for most UNIX systems,
is the beginning of 1970.
If we evaluate the localtime
function in scalar context
(instead of array context), we will get a human readable form
of the date. The following Perl script demonstrates some uses
of the localtime
function:
#!/usr/bin/perl -w
use strict;
my @lt = localtime;
# @lt = ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
print "@lt\n";
print "The current time is ", scalar localtime, "\n";
print scalar localtime 2**31-1, "\n";
print scalar localtime 2**31, "\n";
lt.pl
The above script displays:
23 44 17 6 3 104 2 96 1 The current time is Tue Apr 6 17:44:23 2004 Mon Jan 18 23:44:07 2038 Fri Dec 13 17:15:00 1901
A timestamp, when stored in a signed 32-bit value, can hold the number of seconds from 1970 to early 2038 only. After that, the signed timestamp becomes negative meaning that it will be interpreted as a date in late 1901. Any machines (especially embedded devices) that use signed 32-bit values in 2038 will experience rollover problems.
Consider the case where a programmer wishes to determine the current year, month and date. The following script works correctly, but is inelegant:
#!/usr/bin/perl -w
use strict;
my @time = localtime;
my $year = $time[5] + 1900;
my $month = $time[4] + 1;
my $day = $time[3];
printf "%4d-%02d-%02d\n", $year, $month, $day;
as1.pl
The year stored in the sixth element of the array returned by
localtime
denotes the number of years since 1900,
therefore, we must add 1900 to the year value in order to get a proper
four digit year. We add one to the month since the month returned by
localtime
represents January as 0.
We can make the above script a bit more compact by doing a multiple assignment:
#!/usr/bin/perl -w
use strict;
my @time = localtime;
my ($year, $month, $day) = ($time[5] + 1900, $time[4] + 1, $time[3]);
printf "%4d-%02d-%02d\n", $year, $month, $day;
as2.pl
The assignment is still a bit repetitious. Perl provides a feature known as array slices which allows one to extract a subset of the elements of an array, as demonstrated by the following example:
#!/usr/bin/perl -w
use strict;
my @time = localtime;
my ($year, $month, $day) = @time[5,4,3];
printf "%4d-%02d-%02d\n", $year+1900, $month+1, $day;
as3.pl
Note that we write @time[5,4,3]
and not
$time[5,4,3]
, because the result of an array slice is
another array, not a scalar. Also note that we must remember to add
1900 and 1 to the year and month respectively during our output.
The final exam will cover the entire course (C, C++ and Perl). However, because the midterm was almost entirely C, the number of questions on the final related to C will be relatively small. The emphasis of the exam will be on C++ and Perl. There may also be a question or two related to all three languages, so you should be familiar with the similarities/differences and strengths/weaknesses of each language relative to one another.
The structure of the final exam will likely be similar to the midterm exam, but there will be more questions (maybe eight?). There may even be a few multiple choice questions. You should review all the assignments and their solutions and make sure you understand them. Even though Assignment #9 was optional, you are still responsible for the material covered by that assignment. (Incidentally, if you find any mistakes in the solutions to any of the assignments, please let me know.)
Questions from the final exam will be based upon:
Good luck on your finals!
Last modified: April 7, 2004 14:37:47 NDT (Wednesday)