Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings to All,
I was trying to use the Time::Local 'timelocal_nocheck';module/function.

I wanted to split the result and put each element into a seperate variable.
But everytime I use a single digit number(9) into the $mday location of the
timelocal($sec,$min,$hour,$mday,$mon,$year);format
The print statement is missing the year.
Fri May 9 00:00:00
If I use a double digit(11) $mday everything is great. The date prints with the year
Fri May 9 00:00:00 2003

Why would a single digit or double digit make any difference when the split uses a space as the delimiter.
Thanks
#!/usr/local/bin/perl -w use strict; use diagnostics; use CGI qw(:standard); use Time::Local 'timelocal_nocheck'; my $DatStr1 = localtime timelocal_nocheck 0,0,0,9,4,2003; my @text_dat1 = split( / /, $DatStr1); my $DatStr2 = localtime timelocal_nocheck 0,0,0,11,4,2003; my @text_dat2 = split( / /, $DatStr2); print header(); print "@text_dat1[0] @text_dat1[1] @text_dat1[2] @text_dat1[3] @text_d +at1[4]<br>\n"; print $DatStr1."<br><br>\n"; print "@text_dat2[0] @text_dat2[1] @text_dat2[2] @text_dat2[3] @text_d +at2[4]<br>\n"; print $DatStr2."<br>\n";

Replies are listed 'Best First'.
Re: Problem splitting Time::Local result
by bobn (Chaplain) on Jul 28, 2003 at 05:33 UTC

    you want split(' ', $DatStr1), not split(/ /, $DatStr1) - the first form produces one field from the space in front of 11 or the 2 spaces in front of 9, but the second form creates an extra blank field from the 2 spaces infront of 9.

    In your current code,

    print scalar(@text_dat1), " ", scalar(@text_dat2), "\n";
    produces
    6 5

    Changing the / / to ' ' makes the scalars equal and fixes your problem. 'perldoc -f split' covers this.

    Also, your scalars of form @text_dat2[0] should be $text_dat2[0], as noted by diagnostics.

    --Bob Niederman, http://bob-n.com
Re: Problem splitting Time::Local result
by cleverett (Friar) on Jul 28, 2003 at 16:24 UTC
    The other way to get what you want is:
    my @text_dat1 = split(/\s+/, $DatStr1);

    The regular expression /\s+/ gets satisfied by the longest possible sequence of whitespace possible, whereas / / is only satisfied by a single space.