Re: Manipulating Date Numbers
by derby (Abbot) on Feb 27, 2003 at 19:57 UTC
|
Well, the simple approach is to just split:
#!/usr/bin/perl -w
while( <DATA> ) {
chomp;
($y,$m,$d) = split( /\//, $_ );
print "$m/$d/$y\n";
}
__DATA__
74/05/25
54/03/04
69/08/23
55/08/24
-derby | [reply] [Watch: Dir/Any] [d/l] |
|
I didn't even THINK about split. I hate me, but you rock.
Thanks!
--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.
| [reply] [Watch: Dir/Any] |
Re: Manipulating Date Numbers
by BrowserUk (Patriarch) on Feb 27, 2003 at 19:56 UTC
|
while(<DATA>) {
s[(..)/(../..)][$2/$1];
print;
}
..and remember there are a lot of things monks are supposed to be but lazy is not one of them
Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Manipulating Date Numbers
by Limbic~Region (Chancellor) on Feb 27, 2003 at 20:02 UTC
|
ellem,
The solution I am providing is applicable to field swapping.
If you need to do real date manipulation, I suggest:
- Date::Calc
- Date::Manip
- Time::Local
This is OWTDI - there are plenty though:
#!/usr/bin/perl -w
use strict;
while (<DATA>) {
chomp;
my @fields = split "/" , $_;
print join "/" , @fields[1,2,0];
print "\n";
}
__DATA__
74/05/25
54/03/04
69/08/23
55/08/24
Hope this gets you pointed in the right direction!
Cheers and happy hacking - L~R | [reply] [Watch: Dir/Any] [d/l] |
Re: Manipulating Date Numbers
by tachyon (Chancellor) on Feb 27, 2003 at 20:08 UTC
|
for (<DATA>) {
chomp;
next unless $_;
my ( $yy,$mm,$dd ) = split '/';
print "$mm/$dd/$yy\n";
}
__DATA__
74/05/25
54/03/04
69/08/23
55/08/24
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [Watch: Dir/Any] [d/l] |
Re: Manipulating Date Numbers
by l2kashe (Deacon) on Feb 27, 2003 at 20:41 UTC
|
just because no one has done it in one line yet :P
my @old = ('74/05/25', '54/03/04', '69/08/23', '55/08/24');
my @new = map { join('/', ( split('/') )[1,2,0]) } @old;
Produces:
old: 74/05/25 new: 05/25/74
old: 54/03/04 new: 03/04/54
old: 69/08/23 new: 08/23/69
old: 55/08/24 new: 08/24/55
I dont know if someone could golf this, but *shrug*
/* And the Creator, against his better judgement, wrote man.c */ | [reply] [Watch: Dir/Any] [d/l] |
|
s!(..)/(.*)!$2/$1! for @old;
to edit in place or
my @new = map {s!(..)/(.*)!$2/$1!;$_} @old;
for a new array.
-caedes | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
my@new=map s|(^..)/(.*)|$2/$1|,@old;
or as a filter
C:\test>copy con junk
74/05/25
54/03/04
69/08/23
55/08/24
^Z
1 file(s) copied.
C:\test>perl -pes=(..)/(.*)=$2/$1= <junk
05/25/74
03/04/54
08/23/69
08/24/55
..and remember there are a lot of things monks are supposed to be but lazy is not one of them
Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.
| [reply] [Watch: Dir/Any] [d/l] [select] |