For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > August 2007 > Adding a comma to format localtime









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 Adding a comma to format localtime
Gregg O'Donnell

2007-04-09, 9:55 pm

All,
I use this line of code:
my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];

To create this result:
Mon 9 Apr 2007 09:15:05

How can I add a comma to this result to get:
Mon, 9 Apr 2007 09:15:05

Best and thanks,
Gregg


---------------------------------
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
Wiggins d'Anconia

2007-04-09, 9:55 pm

Gregg O'Donnell wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];


my $localtime = [ split ' ', localtime ];
my $datetime = $localtime->[0] . ', ' . join ' ', @$localtime[2,1,4,3];

TMTOWTDI...

http://danconia.org

>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
>
> Best and thanks,
> Gregg
>

Paul Lalli

2007-04-09, 9:55 pm

On Apr 9, 12:41 pm, gpod...@yahoo.com (Gregg O'Donnell) wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05


You're better off using modules the way they were designed to be used,
rather than hacks like the above.

use POSIX qw/strftime/;
my $datetime = strftime("%a, %e %b %Y %T", localtime);

See also:
man strftime
perldoc POSIX

Paul Lalli

Purl Gurl

2007-04-09, 9:55 pm

Paul Lalli wrote:

> Gregg O'Donnell wrote:


[color=darkred]
[color=darkred]
[color=darkred]
> You're better off using modules the way they were designed to be used,
> rather than hacks like the above.


> use POSIX qw/strftime/;
> my $datetime = strftime("%a, %e %b %Y %T", localtime);



Better off using a module?

#!perl

$datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];

print $datetime;

print "\n\n";

use POSIX qw/strftime/;
$datetime = strftime("%a, %e %b %Y %T", localtime);

print $datetime;


PRINTED RESULTS:

Mon 9 Apr 2007 13:21:25

Mon, Apr 2007


Purl Gurl
Purl Gurl

2007-04-09, 9:55 pm

Gregg O'Donnell wrote:

> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];


> To create this result:
> Mon 9 Apr 2007 09:15:05


> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05



#!perl

$datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];

substr ($datetime, 3, 0, ",");

print $datetime;


PRINTED RESULTS:

Mon, 9 Apr 2007 13:34:01


Purl Gurl
John W. Krahn

2007-04-11, 9:55 pm

Gregg O'Donnell wrote:
>
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05


( my $datetime = localtime ) =~ s{(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)}

{$1, $3 $2 $5 $4};



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
Susheel Koushik

2007-04-12, 3:55 am

use this:

$str = localtime;
@fields = split/ /,$str;
$str2 = "$fields[0]".","." $fields[2]"." $fields[1]"." $fields[4]"."
$fields[3]";
print ("$str2\n");

On 4/9/07, Gregg O'Donnell <gpod363@yahoo.com> wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
>
> Best and thanks,
> Gregg
>
>
> ---------------------------------
> Looking for earth-friendly autos?
> Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.

Paul Archer

2007-04-12, 7:55 am

A cleaner way to do it is:
printf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
Or, if you want to save it to a variable:
my $dateout = sprintf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];

Paul


11:57am, Susheel Koushik wrote:

> use this:
>
> $str = localtime;
> @fields = split/ /,$str;
> $str2 = "$fields[0]".","." $fields[2]"." $fields[1]"." $fields[4]"."
> $fields[3]";
> print ("$str2\n");
>
> On 4/9/07, Gregg O'Donnell <gpod363@yahoo.com> wrote:
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> http://learn.perl.org/
>
>
>




-------------------------------------------------------------------
Perl elegant? Perl is like your grandfather's garage. Sure, he kept
most of it tidy to please your grandmother but there was always one
corner where you could find the most amazing junk. And some days,
when you were particularly lucky, he'd show you how it worked.
----------Shawn Corey <shawn.corey [at] sympatico.ca>--------------
Peter Scott

2007-04-12, 6:55 pm

On Thu, 12 Apr 2007 05:56:14 -0500, Paul Archer wrote:[color=darkred]
> A cleaner way to do it is:
> printf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
> Or, if you want to save it to a variable:
> my $dateout = sprintf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];

Personally, I find this more maintainable and readable:

use POSIX qw(strftime);
my $datetime = strftime "%a, %d %b %Y %T", localtime;

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Haup

2007-05-09, 5:16 pm

http://Angelina-Jolie-doing-it.info...hp?movie=148803
Admap

2007-05-28, 1:19 am

Found this on one of the forums:
Direct access to adult video site's member zone
http://uniqueadult.com/members/video.php?file=1
username: 218571
password: wanttocome
change the number in the link to get other videos!
Carebearcutie

2007-06-27, 7:31 pm

Shania Twain and Angelina Jolie Lesbian XXXXing!
http://www.thetubebender.com/player.php?watch=1673286

Catherine Z. Jones and Ashlee Simpson Spanking And Bdsm Sex!
http://www.thetubebender.com/Play?clip=1673286

Jessica Simpson and Ashlee Simpson Hardest Lesbians Wrestling Match!
http://www.thetubebender.com/PlayMovie?id=1673286

Pamela Anderson and Halle Berry Crazy On High Heels!
http://www.thetubebender.com/PlayMovie.asp?vid=1673286

Jessica Simpson and Mariah Carey , Lesbian Wrestler Loses Fight!
http://www.thetubebender.com/Play?q=1673286

XXXXing funny porn shit video best email funny video funny halo 2 video funny music video clip free funny online video
http://635-funny-video.info/cartoon...-sex-video.html http://635-funny-video.info/free-funny-adult-video.html http://635-funny-video.info/funny-a...video-clip.html http://635-funny-video.info/crazy-e...unny-video.html http://635-funny-video.info/free-fu...line-video.html
Surprise

2007-08-19, 5:15 am

Hot blonde is brutally drilled by a huge dick!
Click here to watch for free - no signup needed
Sponsored Links







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

Copyright 2008 codecomments.com