|
|
Listing C.18 displays the contents of the Perl script splitDate1.pl that demonstrates how to combine arrays, the split function, and the date function. The techniques in this example can be used in a variety of situations.
Listing C.18 splitDate1.pl
my($weekDay) = ""; my($month) = ""; my($monthDay) = ""; my($hrMinSec) = ""; my($timeZone) = ""; my($year) = ""; my($hour) = ""; my($minute) = ""; my($second) = ""; my($line) = ""; my($length) = 0; my(@dateArray); @dateArray = `date`; $length = scalar(@dateArray); $line = join(' ', @dateArray); if( $length == 6 ) { # Unix system ($weekDay,$month,$monthDay,$hrMinSec,$timeZone,$year) = split(/ /, $line); } else { # non-Unix system (e.g., NT) ($weekDay,$month,$monthDay,$hrMinSec,$year) = split(/ /, $line); } ($hour, $minute, $second) = split(/:/, $hrMinSec); print "Today's date: @dateArray\n"; print "Day of Week: $weekDay\n"; print "Month: $month\n"; print "Day of Month: $monthDay\n"; print "Hour(s): $hour\n"; print "Minute(s): $minute\n"; print "Second(s): $second\n"; print "Time Zone: $timeZone\n"; print "Year: $year\n";
You can launch the Perl script splitDate1.pl in Listing C.18 from the command line as follows,
perl -w splitDate1.pl
and the output is as follows:
Today's date: Fri May 23 16:03:40 2003 Day of Week: Fri Month: May Day of Month: 23 Hour(s): 16 Minute(s): 03 Second(s): 40 Time Zone: Year:
|
|