Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Manipulating Date Numbers

by ellem (Hermit)
on Feb 27, 2003 at 19:49 UTC ( [id://239229]=perlquestion: print w/replies, xml ) Need Help??

ellem has asked for the wisdom of the Perl Monks concerning the following question:

Person has reams of Dates in this format:
yy/mm/dd
needs them in
mm/dd/yy

I am thinking a simple s/// or tr/// in a foreach could do the trick but I am having some difficulty pulling it off. It's weird usually I have no idea how to do something, but know the syntax when someone shoves me in the right direction, but not this time.

Here's what the DATA looks like:
_DATA_ 74/05/25 54/03/04 69/08/23 55/08/24
I realized my code is embarrassing and have chosen NOT to show it.
--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
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

      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.
Re: Manipulating Date Numbers
by BrowserUk (Patriarch) on Feb 27, 2003 at 19:56 UTC

    This might be close?

    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.
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

Re: Manipulating Date Numbers
by tachyon (Chancellor) on Feb 27, 2003 at 20:08 UTC

    split will be the easiest way...

    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

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 */
      How about
      s!(..)/(.*)!$2/$1! for @old;
      to edit in place or
      my @new = map {s!(..)/(.*)!$2/$1!;$_} @old;
      for a new array.

      -caedes

      my @new = map { s!(..).(..).(..)!$2/$3/$1!; $_ } @old;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://239229]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found