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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty field check

Status
Not open for further replies.

venkatpavan

Programmer
Feb 18, 2006
42
SG
Hi,

Q1. How to check whether field is empty or not.
Q2. How to check the Date which I received in my file is valid date or not.

Thanks,

 
Below are the couple records from the flat file

123456789,2010-12-03,perl,yes,2010-10-10 13:12:45
,2010-12-03,perl,no,2010-10-10 13:12:45

Field:
Above you can see First field is empty,I need to check whether that field is blank or not,If it is blank I need to do some check.

Date:
Second field is date field,It comes as YYYY-MM-DD format as shown above,I need to check whether date is in above format or not.

Thanks


 
OK so we have established that you are using a flat file, this was not obvious by the initial question.

How are you assigning the data to vars for testing?

Keith
 
After you read the record from your flat file, you can parse it into fields using the comma as your field delimiter. You can also include leading and trailing white-space (\s) as part of the delimiter.

Code:
@fields=split(/\s*,\s*/, $record)


Since the spaces of the first field are consumed as leading spaces of the delimiter, the first field would contain a null string.

Code:
if($fields[0] eq "") {
   print "first field is empty\b";
}

To check if the second field is a date in this or last century, you can try something like this. (I used various notations to represent digits):

Code:
# Note some invalid dates may pass this test like "2010-19-39"

if ($fields[1] !~ m/^[12]\d{3}-[01]\d-[0-3][0-9]$/io) {
   print "Second field is not a valid date\n";
}

I hope this helps

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top