PDA

View Full Version : conditional requiring text match fails


bazz
03-13-2006, 12:27 PM
Having read in a specific line from a list of files, I need to work with that line.

If the line has no value, then it is meant to build @vacantRoom
If the line has a text value then it is meant to compile @closedRoom
If the line has a numerical value, it is meant to build @vacantRoom

It differentiates between an empty and non-empty value but seesm not to identify text -v- numbers. The part - if (bookingReference =~ /^./) is the same as in my other file and it works there.



if (!$bookingReference) {
$countVacancies++;
push (@roomFileHandle, $roomFileHandle);
#print "roomFileHandle = $roomFileHandle<br />\n"; # successful
push(@vacantRooms, $allDatesRequested); #successful
push (@nightsRequested, $actualNightOfStay); #successful
} elsif ($bookingReference =~ /^./) { # to check if first character is a letter
push(@closedRoom, $allDatesRequested);
} elsif ($bookingReference) {
push (@occupiedRooms, $allDatesRequested);
}

nkrgupta
03-13-2006, 12:36 PM
Having read in a specific line from a list of files, I need to work with that line.

If the line has no value, then it is meant to build @vacantRoom
If the line has a text value then it is meant to compile @closedRoom
If the line has a numerical value, it is meant to build @vacantRoom

It differentiates between an empty and non-empty value but seesm not to identify text -v- numbers. The part - if (bookingReference =~ /^./) is the same as in my other file and it works there.



if (!$bookingReference) {
$countVacancies++;
push (@roomFileHandle, $roomFileHandle);
#print "roomFileHandle = $roomFileHandle<br />\n"; # successful
push(@vacantRooms, $allDatesRequested); #successful
push (@nightsRequested, $actualNightOfStay); #successful
} elsif ($bookingReference =~ /^./) { # to check if first character is a letter
push(@closedRoom, $allDatesRequested);
} elsif ($bookingReference) {
push (@occupiedRooms, $allDatesRequested);
}



well... $bookingReference =~ /^./ doesn't check wether the first character is a letter, but it checks if 'anything' ('.' represents 'anything') exists as a first character!

The correct test to check if first character is a letter would be $bookingReference =~ /^[a-z]/
and correct test to check if first character is a digit would be
$bookingReference =~ /^\d/

hth
Naveen

nkrgupta
03-13-2006, 12:39 PM
Here's a handy .txt file (not exhaustive though!) for REGEX basics, in case anyone requires them (and all would do so, if perl is to be mastered :D )

bazz
03-13-2006, 04:23 PM
Blimey how'd I miss that. Thanks.

I think I'll put that down to a grey, wet, crappy Monday Morning :)

bazz