Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Syntax problem

Reply
Thread Tools

Syntax problem

 
 
Glenn J. Rowe
Guest
Posts: n/a
 
      12-05-2003
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



 
Reply With Quote
 
 
 
 
Robert Dover
Guest
Posts: n/a
 
      12-05-2003
Glenn J. Rowe wrote:

> 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

 
Reply With Quote
 
 
 
 
Robert Dover
Guest
Posts: n/a
 
      12-05-2003
Robert Dover wrote:

> 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

 
Reply With Quote
 
John J. Trammell
Guest
Posts: n/a
 
      12-05-2003
On Fri, 5 Dec 2003 14:16:09 -0500, Glenn J. Rowe wrote:
> 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.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-05-2003

"Glenn J. Rowe" <undisclosed_sender@undisclosed_domain.com> wrote:
> 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

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
http://www.velocityreviews.com/forums/(E-Mail Removed) <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
 
Reply With Quote
 
Glenn J. Rowe
Guest
Posts: n/a
 
      12-05-2003
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



"Robert Dover" <(E-Mail Removed)> wrote in message
news:bqqohe$ns9$(E-Mail Removed)...
> Robert Dover wrote:
>
> > 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
>



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-06-2003
Glenn J. Rowe <undisclosed_sender@undisclosed_domain.com> wrote:

> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-06-2003
Glenn J. Rowe wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments