Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl - Looping while statements - Enter valid dates

Posted on 2007-03-27
6
Medium Priority
?
140 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 200 total points
ID: 18803040
$startdate ne
should probably be
$startdate !~
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

715 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