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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using date format as a regular expression

Status
Not open for further replies.

melsterTEK

Programmer
Feb 6, 2007
27
0
0
US
Hi I need help in writing this perl code for matching a string with today's date.

I have the following code that gives me date:
#!/usr/bin/perl

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
printf "%4d-%02d-%02d\n",
$year+1900,$mon+1,$mday;

Result => 2007-02-20

I want to take this and add this to my pattern match which I have so far..

$sth->execute;

# fetch rows
while (($errorlog,
$continuationrow
)= $sth->fetchrow_array( )) {
if ($errorlog =~ /Operating system/) {
$data = $data."$_ Errors found:$errorlog\n";
}
}

What I want to do is get the errors found in $errorlog where date is todays date in this format (2007-02-20).

Thanks!
 
Can you show me how to write that? I'm still unclear on how to plug this in. Thanks.

Looking at the sample from perl doc:

# Format number with up to 8 leading zeroes
$result = sprintf("%08d", $number);

# Round number to 3 digits after decimal point
$rounded = sprintf("%.3f", $number);

================
Here's my loop:
================
while (($errorlog,
$continuationrow
)= $sth->fetchrow_array( )) {
if ($errorlog =~ /Operating system|^Error: 1102/) {
$data = $data."$_ Errors found:$errorlog\n";
}
}
 
I think I worked it out...

(@date)=localtime;
$date[5]+=1900;$date[4]++;
$today=sprintf("%0.4d-%0.2d-%0.2d",$date[5],$date[4]++, $date[3]-1);


# fetch rows
while (($errorlog,
$continuationrow
)= $sth->fetchrow_array( )) {

if ($errorlog =~ /$today/) {
if ($errorlog =~ /Operating system|^Error: 1102/) {
$data = $data."$_ Errors found:$errorlog\n";
}
}
}
 
melsterTEK said:
I think I worked it out...

(@date)=localtime;
$date[5]+=1900;$date[4]++;
$today=sprintf("%0.4d-%0.2d-%0.2d",$date[5],$date[4]++, $date[3]-1);

It looks like you struggled a little getting it to work based of your double use of $date[4]++. Just note that ++ after a variable means that the increment will be done last, as in after the enclosing statement, while a ++ first will be done before the enclosing statement.

IE:

some random statement ... $date++ ... more statement;
is equivalent to
some random statement ... $date ... more statement;
$date += 1;

some random statement ... ++$date ... more statement;
is equivalent to
$date += 1;
some random statement ... $date ... more statement;

Note the order of operations.

Anyway, all you had to do was plug in sprintf where you had printf before, then include it in a regex like you've now done:

Code:
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
my $today = sprintf("%4d-%02d-%02d\n", $year+1900, $mon+1, $mday);

Well done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top