Syntax problem

G

Glenn J. Rowe

I need to read in a variable and make sure it is in the exact format of
YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26


I tried the following code but it doesn't work...
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
{
#Continue with code
}
else
{
#Display an error
}

I am a self taught programmer so any help you can give me would be
appreciated.

Thanks,

Glenn
 
R

Robert Dover

Glenn said:
I need to read in a variable and make sure it is in the exact format of
YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26


I tried the following code but it doesn't work...
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
{
#Continue with code
}
else
{
#Display an error
}

I didn't rigorously test it, but it seems to work for me. What problems are you
seeing?

-Bob
 
R

Robert Dover

Robert said:
Glenn J. Rowe wrote:

I didn't rigorously test it, but it seems to work for me. What problems
are you seeing?

Sorry to follow-up on my own post, but here's what I did:

#!/usr/bin/perl -w
$variable = "2003_DEC_05_14:05:26";
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
{print "ok\n";}
else {print "nope\n";}

Some points:

-Your regex will also match "abc 1234_SOP_13_99:99:99 something else".
-If you're getting your $variable from STDIN, don't forget to chomp it.

HTH,
BD
 
J

John J. Trammell

I need to read in a variable and make sure it is in the exact format
of YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26

I tried the following code but it doesn't work...

[aside: "doesn't work" is usually not the best choice of
phrase to describe a programming problem...]
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)

Apart from removing the unnecessary parens, I would skip the {N}
and add ^, $ anchors for clarity (assuming that doesn't cause
other problems), and maybe tweak the "month" field:

/^\d\d\d\d_\w\w\w_\d\d_\d\d:\d\d:\d\d$/ # untested

Of course this doesn't verify that you have a valid date at all.

But I doubt this would fix your "doesn't work" problem. Code like:

warn "\$variable is '$variable'";
if ($variable =~ /whatever/) { ... }

can help determine exactly what text you're using in the match.
 
B

Ben Morrow

Glenn J. Rowe said:
I need to read in a variable and make sure it is in the exact format of
YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26

I tried the following code but it doesn't work...
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)

my $month = join "|", qw/JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/;

if( $variable =~ /^ (\d{4}) _ ($month) _ (\d{2}) _
(\d{2}) : (\d{2}) : (\d{2}) $/xo and
$1 > 1900 and $1 < 2500 and $3 > 0 and $3 < 32 and
$4 >= 0 and $4 < 24 and $5 >= 0 and $5 < 60 and $6 >= 0 and $6 < 60 ) {

#pass
} else {
#fail
}

Or I would use one of the date-parsing modules from CPAN.

Ben
 
G

Glenn J. Rowe

Hmmm - I "just" noticed something...

Up till now I was testing the code by changing the first part to either 203
or 20003 instead of 2003 and the code didn't work.

I just tried messing up other parts and it worked fine. (ie. NOV changed to
NV)

I changed the code to
if($variable=~m/2(\d{3})_(\D{3})_(\d{2})_(\d{2}):(\d{2}):(\d{2})/) and all
is fine.

Not sure why it only works if I specify the first character but hey it
works.

Thanks for your help,

Glenn
 
T

Tad McClellan

Glenn J. Rowe said:
Subject: Syntax problem


You do not have a syntax problem, your code compiles just fine.

It must be some other kind of problem.

I need to read in a variable and make sure it is in the exact format of
YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26


I tried the following code but it doesn't work...


What does "doesn't work" mean when you say it?

if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)


We need two things (a string and a pattern) in order to evaluate the
behavior of a pattern match, but we only have one of them.

What value is in $variable?

Your pattern looks fine to me (apart from a bunch of parenthesis
that are not needed).

I am a self taught programmer so any help you can give me would be
appreciated.


Read the Posting Guidelines that are posted here frequently.
 
J

Jürgen Exner

Glenn said:
I need to read in a variable and make sure it is in the exact format
of YYYY_MMM_DD_hh:mm:ss

A namesake of yours just posted exactly the same question to alt.perl.
You may want to check there for answers.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top