Solved

Perl - Looping while statements - Enter valid dates

Posted on 2007-03-27
6
133 Views
Last Modified: 2010-04-16
I'm attempting to write a perl script with two looping while statements that prompts a user for information such as the effective starting and ending dates for a promotion.  Since this information is going to be used to edit an xml file I need this information in the format of %4d-%02d-%02d (with the hyphens).  Below is my first attempt at this, which I can't figure out why it does not work:

print "Please enter the effective start date for this promotion.\n";
    print "Enter a date numerically: yyyy-mm-dd\n";
    print "example 2005-12-31\n";
    $startdate = <STDIN>;
    chomp $startdate;

while ($startdate ne /^([0-9]{4})\/([0-12]{2})\/([0-31]{2})$/){
 print "Invalid Entry. Please try again.\n";
 $startdate = <STDIN>;
}
print "Please enter the expiry date for this promotion.\n";
    print "Enter a date numerically: yyyy-mm-dd\n";
    print "example 2005-12-31\n";
    $enddate = <STDIN>;
    chomp $enddate;

while ($enddate ne /^([0-9]{4})\/([0-12]{2})\/([0-31]{2})$/){
 print "Invalid Entry. Please try again.\n";
 $enddate = <STDIN>;
}


Any information is greatly appreciated!
0
Comment
Question by:gibsonvc
  • 4
  • 2
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 18803040
$startdate ne
should probably be
$startdate !~
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 18803083
[0-12]{2}
acceps any character between 0 and 1 or 2, repeated twice
Ypu may have wanted
(\d|1[0-2])
or
(0?\d|1[0-2]

[0-31]{2} accepts any character between 0 and 3 or 1, repeated twice
you may have wanted
(\d|[12]\d|3[01])
or maybe
(0?[1-9]|[12]\d|3[01])
0
 

Author Comment

by:gibsonvc
ID: 18803090
I apologize I'm still very new to scripting.
I have changed the ne to !~ but it still will not allow accept valid or invalid entries!?!?

 
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
ID: 18803230
what entries is it not accepting?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18803245
your print saays to use - between numbers but your expression looks for /
0
 

Author Comment

by:gibsonvc
ID: 18803348
Thank you, it works now!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now