Solved

Perl - Looping while statements - Enter valid dates

Posted on 2007-03-27
6
137 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
Industry Leaders: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

733 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