Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There's got to be an easier way to do this. STDIN and sprintf question

Status
Not open for further replies.

StormMedic85

Programmer
Sep 7, 2007
20
US
Below is my current code. I have the user enter a year, month, date and time. What I'd like to do is consolidate this all into one line...the user can enter it in a certain format like YYYYMMDDTT and the program will extract the year, month, and date from what's entered and format it the same way I have it below.

The formatting is important. For example, if the user were to just enter 9 for the month and not 09 I would want the program to format it appropriately. Any suggestions and help would be greatly appreciated!

Here's how I have it currently...

#!/usr/local/bin/perl -w

print "Enter the four digit year: ";
$year = <STDIN>;
chomp $year;

print "Enter the number of the month: ";
$month = <STDIN>;
chomp $month;

print "Enter the date: ";
$date = <STDIN>;
chomp $date;

print "Enter the sounding time (1200 or 0000): ";
$time = <STDIN>;
chomp $time;

$RAMSDate=sprintf("%04d-%02d-%02d-%04d",$year,$month,$date,$time);
$NWSDate=sprintf("%04d%02d%02d%04d",$year,$month,$date,$time);

print "$RAMSDate \n";
print "$NWSDate \n";
 
I agree with audiopro that 3 popup menus would be easiest.

However I guess you could use Perls split function to split the year, month, date, and time.

You would therefore need a character between each of the elements (a separator) so that perl can identify each individual elements...

So... YYYY/MM/DD/TT (So the separating character here is "," (comma).

So, the user enters the information into a textbox in that format, i.e. they enter "2007,09,24,12"...

#variables
my ($data, @data, $year, $month, $day, $time);

#Take the data from the textbox
$data = param('textbox');

#split it into the individual elements
@data = split (/,/ , $data);

#print the data
foreach (@data) {
($year, $month, $day, $time) = split (/,/);
}

I have only quickly written this, so I may have made an error. But I think using the split function will help you here

Chris


 
You have to validate each field of the date/time before handing it off to sprintf for further formatting. Otherwise you could just be formatting bad date/times. What is someone accidently enters 33 for the month?

Anyway you do it, seperate menus or one, you have to validate the user input before proceeding.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thats another issue. It is usually best to allow users to select from a set number of years because years are pretty much infinate. Days you could set it so the number must be between 1 and 31 (so $### >= 1 || $### <= 31) however you still have the issue that there are not always 31 days in a month. Do the same for days and time.
I always get the solution I need, but I am not expereinced enough to write shorter code.
Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top