The Perl split Function and Text Strings

   



Combining split with Other Functions

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

start example
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";
end example

Remarks

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:             



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net