Smart method to reformat date

S

SaltyBall

Hi,

text file reformat
from
2006-12-25,xxx,yyy...
to
25/12/2006,xxx,yyy...

I think I can use "split" to kick it to an array line by line and
restructure it but I think it should be some smart method to do that.

Can I do it with =~ and regular expression or any other smart method?

Thanks very much!
 
D

David Squire

SaltyBall said:
Hi,

text file reformat
from
2006-12-25,xxx,yyy...
to
25/12/2006,xxx,yyy...

I think I can use "split" to kick it to an array line by line and
restructure it but I think it should be some smart method to do that.

There are modules available on CPAN that do all sorts of date and time
formatting an processing. See for example Date::Manip.

Can I do it with =~ and regular expression or any other smart method?

If you *always* get your strings in the format above, you can do:

----

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
s|^([0-9]{4})-([0-9]{2})-([0-9]{2})|$1/$2/$3|;
print;
}


__DATA__
2006-12-25,xxx,yyy...
2005-09-01,xaaxx,bbb..
1969-04-01 April Fool!

----

Output:

2006/12/25,xxx,yyy...
2005/09/01,xaaxx,bbb..
1969/04/01 April Fool!


Regards,

DS
 
S

SaltyBall

Thanks very much!

It works, the usage of s|| and {} is new to me
(I am a newbie and only have a copy of "Learning perl" at hand)

can you recommend some good web site so that we can get more info?

Thanks again



David said:
SaltyBall said:
Hi,

text file reformat
from
2006-12-25,xxx,yyy...
to
25/12/2006,xxx,yyy...

I think I can use "split" to kick it to an array line by line and
restructure it but I think it should be some smart method to do that.

There are modules available on CPAN that do all sorts of date and time
formatting an processing. See for example Date::Manip.

Can I do it with =~ and regular expression or any other smart method?

If you *always* get your strings in the format above, you can do:

----

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
s|^([0-9]{4})-([0-9]{2})-([0-9]{2})|$1/$2/$3|;
print;
}


__DATA__
2006-12-25,xxx,yyy...
2005-09-01,xaaxx,bbb..
1969-04-01 April Fool!

----

Output:

2006/12/25,xxx,yyy...
2005/09/01,xaaxx,bbb..
1969/04/01 April Fool!


Regards,

DS
 
D

David Squire

SaltyBall wrote:

[please don't top-post. top-posting corrected]

[snip]
Can I do it with =~ and regular expression or any other smart method?

If you *always* get your strings in the format above, you can do:

----

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
s|^([0-9]{4})-([0-9]{2})-([0-9]{2})|$1/$2/$3|;
print;
}


__DATA__
2006-12-25,xxx,yyy...
2005-09-01,xaaxx,bbb..
1969-04-01 April Fool!

Thanks very much!

It works, the usage of s||

The '|'s are just delimiters. You can choose your own delimiters when
using s and m. I chose '|' because I wanted to use '/' in the
replacement pattern (you will see that '/'s are the most commonly used
delimiters in examples).
and {} is new to me
(I am a newbie and only have a copy of "Learning perl" at hand)

can you recommend some good web site so that we can get more info?

Perl come with its own documentation. It is accessible by typing
'perldoc <manual_entry>' on the command line, where '<manual_entry>' is
the bit you want to read. I strongly suggest that you read:

perldoc perlretut
perldoc perlre

Note that this documentation is also available on-line at
http://www.perl.com/pub/q/documentation, which is perhaps easier as you
don't need to know the names of the manual sections in advance.


DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top