Also, your regular expression has some issues. Let's improve it incrementally. The value I get in
$label at the current time is
Mon 01/15/2001. Assuming that's correct, let's patch this up:
- You have two forward slashes '/' escaped. Rather than escape them, change the delimeters on the substitution for clarity:
$label=~ s#.*\s(\d+)/(\d+)/#\1\2#;
Right off the bat, we have a huge improvement in terms of "readability".
- \1 and \2 should not be in the right side of a substitution. They are used within a regular expression to identify a backreference. This implies to me that you are not using warnings. If you are, you get a message like "\1 better written as $1." Let's go ahead and change those:
$label=~ s#.*\s(\d+)/(\d+)/#$1$2#;
Not only does that eliminate the warning, it gets rid of a couple of escapes that might obscure the meaning to a novice programmer.
- And finally (you old-timers knew I was going to bring this up :-), you have a dot star '.*' in your regex. While dot star is sometimes useful (when you're slurping up the rest of a string, for example), this combination usually kills the efficiency of a regular expression. My usual comment: see Death to Dot Star! for details of this. Change the dot star to a negated character class (or a non-whitespace metacharacter '\S', in this case). If you are iterating over this expression, you may see a significant performance increase.
$label=~ s#\S*\s(\d+)/(\d+)/#$1$2#;
We went from this:
s/.*\s(\d+)\/(\d+)\//\1\2/;
To this:
s#\S*\s(\d+)/(\d+)/#$1$2#;
It might not seem like much for such a small regex, but as they get more complicated, such small changes can make a world of difference.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.