Solved

Perl - Looping while statements - Enter valid dates

Posted on 2007-03-27
6
139 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

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

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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