There's a couple of ways to do this, using either a regex or (if your dates are always well-formed) 'split.' If you use a regex you need to do two things: 1) add a ^ character to anchor your regex to the beginning of your scalar. As it is your regex will match ANYWHERE in the string, not just the beginning. Using the ^ character you can restrict the match to the beginning of the string, which is what you want. Also, 2) you'll probably need to add capturing parentheses around the parts of the regex you're interested in. A matching regex without capturing parentheses only returns a "true" or "false" depending on whether a match is found or not. It does NOT return the match itself. (Well it does, but only if you use some funny variables... not recommended.) Like this:
#!/usr/bin/perl -w
use strict;
my $date= 'Sun Apr 1 10:27:03 CDT 2001';
if ($date =~ m/^(\w{3}\s+\w{3}\s+\d+)/) {
print "Matched $1\n";
}
Alternatively, you can use 'split' (this would be my choice.) Split will split the string up into space-divided chunks... so long as the order of the chunks doesn't change it's all good:
my $date= 'Sun Apr 1 10:27:03 CDT 2001';
my ($weekday, $month, $day) = split " ", $date;
print "$weekday $month $day\n";
Perlman perlfunc has more details on split if you're interested. Enjoy!
Gary Blackburn
Trained Killer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.