Solved

Perl - Looping while statements - Enter valid dates

Posted on 2007-03-27
6
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

728 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