For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > Problems creating a simple variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problems creating a simple variable
Dave Adams

2005-07-24, 8:29 pm

I want to create a variable, to be used for other stuff, using today's date=
..

Can anyone help me with the second last line?

use Time::localtime;
my $tm =3D localtime;
printf("The current date is
%04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
my $currentdate =3D ??????
print ($currentdate);


HSCH (Hope Someone Can Help)
Jm

2005-07-24, 8:29 pm

not sure how you would work your version, but here is what i use all
the time, maybe it will be easier for you as well...

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =3D localt=
ime;
my $am_pm =3D $hour > 11 ? " PM" : " AM";
$year +=3D 1900;
$mon +=3D 1;
$sec =3D "0" . $sec if $sec < 10;
$min =3D "0" . $min if $min < 10;
$hour %=3D ($hour =3D=3D 12 and $am_pm eq " PM") ? 13 : 12;
$hour =3D "0" . $hour if $hour < 10;
$mday =3D "0" . $mday if $mday < 10;
$mon =3D "0" . $mon if $mon < 10;


On 7/21/05, Dave Adams <davidlamontadams@gmail.com> wrote:
> I want to create a variable, to be used for other stuff, using today's da=

te.
>=20
> Can anyone help me with the second last line?
>=20
> use Time::localtime;
> my $tm =3D localtime;
> printf("The current date is
> %04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
> my $currentdate =3D ??????
> print ($currentdate);
>=20
>=20
> HSCH (Hope Someone Can Help)
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>

Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

On Jul 21, Dave Adams said:

> I want to create a variable, to be used for other stuff, using today's date.


And what do you want it to look like? The YYYYMMDD format you have
printing?

> use Time::localtime;
> my $tm = localtime;
> printf("The current date is
> %04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
> my $currentdate = ??????
> print ($currentdate);


my $currentdate = sprintf "%04d%02d%02d", $tm->year+1900, ...;

You want sprintf() instead of print(), if you want to store the formatted
string instead of print it.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Dave W Turner

2005-07-24, 8:29 pm

Huh? What do you want it to be?

On 7/21/05, Dave Adams <davidlamontadams@gmail.com> wrote:
> I want to create a variable, to be used for other stuff, using today's da=

te.
>=20
> Can anyone help me with the second last line?
>=20
> use Time::localtime;
> my $tm =3D localtime;
> printf("The current date is
> %04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
> my $currentdate =3D ??????
> print ($currentdate);
>=20
>=20
> HSCH (Hope Someone Can Help)
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20



--=20

Dave
All us base are belong to you.
JupiterHost.Net

2005-07-24, 8:29 pm



Dave Adams wrote:
> I want to create a variable, to be used for other stuff, using today's date.
>
> Can anyone help me with the second last line?


sure :)

my $currentdate = '20050721';

You don't explain what you are trying to get as most of us can't read
your mind or have time to extrapolate from your obscure example what
you're trying to do.

Please do a full script/command using strict and warnings ...

> use Time::localtime;
> my $tm = localtime;
> printf("The current date is %04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
> my $currentdate = ??????
> print ($currentdate);


print $currentdate; # ie no parens...

I assume you want to get what printf is printing into $currenttime, in
that case

#!/usr/bin/perl

use strict;
use warnings;
use Time::localtime;

my $tm = localtime;

my $currenttime = sprintf("The current date is
%04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);

print $currenttime;

outputs:
The current date is 20050721


perldoc -f sprintf

HTH :)
Danijel Tasov

2005-07-24, 8:29 pm

davidlamontadams@gmail.com (Dave Adams) wrote:
> use Time::localtime;
> my $tm = localtime;
> printf("The current date is
> %04d%02d%02d\n",$tm->year+1900,($tm->mon)+1, $tm->mday);
> my $currentdate = ??????
> print ($currentdate);


As others mentioned, you need sprintf. But just another hint:

#!/usr/bin/perl

use POSIX();

$currentdate = POSIX::strftime("%Y%m%d", localtime(time));
print $currentdate."\n";
__END__

-DaTa
Danijel Tasov
<dtasov@arcor.de>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com