Chas Owens wrote:
On 9/7/07, Beginner <[EMAIL PROTECTED]> wrote:
On 7 Sep 2007 at 9:47, Chas Owens wrote:

On 9/7/07, Chas Owens <[EMAIL PROTECTED]> wrote:
snip
#FIXME: this creates invalid dates like 2007-02-30
sub rand_date {
        sprintf "%04d-%02d-%02d",
                2000 + int rand 7,
                1 + int rand 12,
                1 + int rand 31;
}
snip

Actually that sub can't return "2007-02-30", kudos to anyone else who
can spot why.
No return statement?

Nope, Perl returns the value of the last executed statement.  This is
useful for when I am feeling lazy (notice the bad use of IO::All) and
don't want to type return.  The code works (it returns dates strings),
but it can't return the string I mentioned in the comment (due to my
being an idiot).

... which no one here will dispute!   :-)

And the fix for your code:

use POSIX 'strftime';
sub rand_date {
    strftime '%Y-%m-%d', gmtime 946684800 + int rand 220838400
    }



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to