will1383
asked on
How do I approach this?
I am working on a small project (which will expand) in order to learn how to user Perl. I have prior software experience so I understand all of the usual methodologies but I do not understand how to make the most efficient use of Perl's powerful features. This is the beginning of this adventure. I hope you can help.
Here's the start:
I have a filename that looks like the following: Server01__NEWFIRST.2005.02 .14.16.10. rpy
The date/timestamp in that name is as follows: yyyy.mm.dd.hh.mm
From that I want to create this:
2-14-2005
Now how in the world do I perform this using the regular expressions? I have the Perl 5 by example book in softcopy and I have been reading through the regular expression stuff but I still do not understand how to apply as there are very few examples.
Any help in educating me is greatly appreciated. A solution is great, but I would like help in learning how the solution comes about.
Thanks in advance.
Here's the start:
I have a filename that looks like the following: Server01__NEWFIRST.2005.02
The date/timestamp in that name is as follows: yyyy.mm.dd.hh.mm
From that I want to create this:
2-14-2005
Now how in the world do I perform this using the regular expressions? I have the Perl 5 by example book in softcopy and I have been reading through the regular expression stuff but I still do not understand how to apply as there are very few examples.
Any help in educating me is greatly appreciated. A solution is great, but I would like help in learning how the solution comes about.
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi will1383. hope this has helped you.
ASKER
yes it has sorry. I haven't had a chance to come back.
I'm still trying to understand how the regular expressions work. This is a good start.
They seem quite complicated on the surface due to their lack of readability.
I'm still trying to understand how the regular expressions work. This is a good start.
They seem quite complicated on the surface due to their lack of readability.
yes they can appear a bit cryptic. If yopu use the x option, you can embed comments in the regex to make it more redable.
$str =~ /
^.* # start with any characters
(\d{4}) # then 4 digits, capture in $1
\. # then literal dot, backslash delimits
(\d{2}) # then 2 digits
\. # then a dot
(\d{2}) # then 2 digits
/x;
$str =~ /
^.* # start with any characters
(\d{4}) # then 4 digits, capture in $1
\. # then literal dot, backslash delimits
(\d{2}) # then 2 digits
\. # then a dot
(\d{2}) # then 2 digits
/x;
ASKER
ooo that's a great little option there. Thank you for that tip. I know I'm going to be bugging you guys quite a bit more so I can work on my regular expression education.
I appreciate it mucho.
I appreciate it mucho.
$str = "Server01__NEWFIRST.2005.0
$str =~ /^.*(\d{4})\.(\d{2})\.(\d{
$year = $1;
$month = $2;
$month = sprintf("%d",$month);
$day = $3;
$new_str = "$month\-$day\-$year";
print $new_str;