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

I am trying to split the date and display it on the top of a web page. I thaught everything was fine until the first of the month hit and my date bombed out. Here is the code:
($Day, $Month, $DayNum, $Time, $Zone, $Year) = split(/ /, `date`);

I know what is happening. The day number goes to a single digit and leaves an additional space in the field which gets split into its own variable and moves the others back in the lineup. How do you split on multiple spaces. Thanks in advance.

Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: Splittin the Date
by merlyn (Sage) on Mar 01, 2001 at 19:31 UTC
    There's about four different ways to answer you, and you'll probably get all four within the next 20 minutes.

    "How do you split on multiple spaces":

    my ($day, $month, $daynum, $time, $zone, $year) = split /\s+/, `date`;

    However, that doesn't go far enough, since you don't actually need to fork here:

    my ($day, $month, $daynum, $time, $zone, $year) = split /\s+/, localti +me;
    However what you probably want to do instead is just take the list output of localtime:
    my @now = localtime;
    Which is already split up nicely. See the docs for details.

    -- Randal L. Schwartz, Perl hacker

Re: Splittin the Date
by jeroenes (Priest) on Mar 01, 2001 at 19:27 UTC
    Just use split(/\s+/, `date`);.

    You can rewrite that to

    @a = split for (`date`);
    If you like that better. If you look at split, it tells you that it uses whitespace as its default delimiter.

    Hopethis helps,

    Jeroen
    "We are not alone"(FZ)

      The guys that port this to Win32 without thinking are going to wonder why it hangs at `date`. On dos, date waits for input.
Re: Splittin the Date
by toadi (Chaplain) on Mar 01, 2001 at 19:44 UTC
    Well I was troubled by that problem too.

    What did I do(I only needed the month,day and year)...

    my date = scalar localtime; my($month,$day,$year) = unpack("x4 a3 x1 a2 x10 a4",$date);
    So maybe it's also good to check the unpack function. It's used when you got fixed length thingies to parse.


    --
    My opinions may have changed,
    but not the fact that I am right

Re: Splittin the Date
by Prince99 (Scribe) on Mar 01, 2001 at 19:42 UTC
    Thanks guys. Your advice took care of the problem.

    Prince99

    Too Much is never enough...