Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem coding new file layout 3

Status
Not open for further replies.

jfite

Technical User
Joined
Jan 12, 2007
Messages
1
Location
US
Hello all. I was hoping I could rack some professional minds about modifying some code. I'm not a programmer, but can read some languages to an extent and make simple mods, but I really need some help on this one.

I have a conversion that would take an 8 digit number for a birthday, YYYYMMDD and rearrange it to MMDDYYYY.
Original code:
Code:
my ( $year, $date ) = unpack( qq{A4 A4}, $revBirthDate );
    $birthDay = ( $date . $year );

What I'm facing now is a change in the file layout that I convert. I need to change the format (M)M/(D)D/YYYY to my usual MMDDYYYY. The parantheses are only there if it is actually a two digit date, so I need to convert 1,2,3 to 01,02,03 etc. I am also faced with the problem of the "/" character and need to get rid of that.

Appreciate any help.

-J
 
Hi

Some code..It might help you.

Code:
#YYYYMMDD and rearrange it to MMDDYYYY

my $birthdate = '2006/7/23';
$birthdate =~ s{(\d+)/(\d+)/(\d+)}{$2/$3/$1};
$mm = sprintf("%02d",$2);
$dd = sprintf("%02d",$3);
$birthdate=$mm.$dd.$1;
print "$birthdate\n";

dmazzini
GSM System and Telecomm Consultant

 
or without the temp variables:

Code:
my $birthdate = '2006/7/23';
$birthdate =~ s|(\d+)/(\d+)/(\d+)|sprintf("%02d",$2).sprintf("%02d",$3).$1|e;
print "$birthdate\n";



- Kevin, perl coder unexceptional!
 
You're the man Kevin!

I did not know how to concatenate the reg expression with the sprint function.

A nice one!





dmazzini
GSM System and Telecomm Consultant

 
thanks, but you really did all the work. Here's another honorary star for your post (I already gave you one so can't give anymore real ones):
star.gif


heck, since they are honorary, here's a bunch of them:

star.gif
star.gif
star.gif
star.gif
star.gif


[smile]


- Kevin, perl coder unexceptional!
 
And maybe a little more concise:

Code:
my $birthdate = '2006/7/23';
$birthdate =~ s|(\d+)/(\d+)/(\d+)|sprintf("%02d%02d%s",$2,$3,$1)|e;
print "$birthdate\n";
 
star fest! [smile]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top