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

Sub providing inconsistent output.

Status
Not open for further replies.

perlescent

Technical User
Apr 7, 2005
23
GB
Hello people: long time since I've been here.

I have a problem.

My sub routine:
reads in a list of filenames
splits them into different vars
lists them in date order
except that for March 2007 and May 2008, it outputs the order wrongly. All filenames were created by the same script - so their composition is consistent.

Can any of you please, point me to what the problem is.

here's the sub.

Code:
sub newroomstatus {
my $numericalMonthOfYear;
my $dateOfMonth   = $query->param('DatesOfMonth');
if (!$dateOfMonth) {
$dateOfMonth = (strftime("%d", localtime));
#print "dateOfMonth =$dateOfMonth\n";
}
my $monthOfYear   = $query->param('MonthsOfYear');
if (!$monthOfYear) {
$monthOfYear = (strftime("%B", localtime));
#print "Month = $monthOfYear";
}

my $yearOfCentury = $query->param('YearsAhead'); 
if (!$yearOfCentury) {
$yearOfCentury = (strftime("%Y", localtime));
#print "yearOfCentury = $yearOfCentury";
}

my $date = $dateOfMonth;
my $last = substr($date, -1, length($date)); #Get the last digit
my $first = substr($date, 0, 1); #for dates that are like: 01,02,03 etc.
if($first == 0){$date = $last;} #change them to: 1,2,3 etc.

unless(($first == 1)&&(length($date) == 2)){ #take care of the "teens"
if($last == 0){$date = $date."th";}
if($last == 1){$date = $date."st";}
if($last == 2){$date = $date."nd";}
if($last == 3){$date = $date."rd";}
if($last == 4){$date = $date."th";}
if($last == 5){$date = $date."th";}
if($last == 6){$date = $date."th";}
if($last == 7){$date = $date."th";}
if($last == 8){$date = $date."th";}
if($last == 9){$date = $date."th";}
}
else
{
$date = $date."th";
}




if ($monthOfYear eq "January") {
$numericalMonthOfYear = "01";} 
if ($monthOfYear eq "February") {
$numericalMonthOfYear = "02";} 
if ($monthOfYear eq "March") {
$numericalMonthOfYear = "03";} 
if ($monthOfYear eq "April") {
$numericalMonthOfYear = "04";} 
if ($monthOfYear eq "May") {
$numericalMonthOfYear = "05";} 
if ($monthOfYear eq "June") {
$numericalMonthOfYear = "06";} 
if ($monthOfYear eq "July") {
$numericalMonthOfYear = "07";} 
if ($monthOfYear eq "August") {
$numericalMonthOfYear = "08";} 
if ($monthOfYear eq "September") {
$numericalMonthOfYear = "09";} 
if ($monthOfYear eq "October") {
$numericalMonthOfYear = "10";} 
if ($monthOfYear eq "November") {
$numericalMonthOfYear = "11";} 
if ($monthOfYear eq "December") {
$numericalMonthOfYear = "12";} 

my $dateBeingChecked = join "-", ($yearOfCentury, $numericalMonthOfYear, $dateOfMonth);
my $normalDateFormat = join " ", ($date, $monthOfYear, $yearOfCentury);
#print "dateBeingChecked = $dateBeingChecked\n";


my $Filepath = "/domains/574/2317/html/cgi-bin/$clientsReservationsDir/Rooms_Booked/"; # to read in dirs for all dates. 
my %BookedDates=();
opendir(my $dir,$Filepath);
while(my $datesBooked=readdir($dir)){
   next unless -d $Filepath.$datesBooked && $datesBooked!~/^\.+$/;
push(@BookedDates,$datesBooked);
}
closedir($dir);
#


foreach my $BookedDates (sort @BookedDates ) {
   chomp;
my $TitleDate= join " ", $MonthForBooking, $YearForBooking;

if ($BookedDates eq $yearOfCentury ) { #only output for the year chosen.



my $Filepath ="/domains/574/2317/html/cgi-bin/$clientsReservationsDir/Rooms_Booked/$BookedDates/"; # location of where to read thru filename for all booked rooms

#print "bookedDates = $BookedDates<br />\n";
opendir(my $dir,$Filepath);
while(my $roomsBooked=readdir($dir)){
   next unless -f $Filepath.$roomsBooked && $roomsBooked!~/^\.+$/;
push(@rooms,$roomsBooked);
}
closedir($dir); # All booked dates are now read in.

print <<EOF;

<h3>$pageTitle</h3>
<table id="table"><tr><th colspan="2">$monthOfYear $yearOfCentury</th></tr>
EOF

foreach (sort @rooms ) { # sort thru the booked rooms files
   chomp;
my ($roomType, $roomNumber, $roomDate) = split /_/;
   $rooms{$roomNumber}{$roomDate}{$roomType}= $_;

} # end foreach @rooms

  foreach my $roomNumber ( sort keys %rooms) {


print <<eof;
<tr><th>$roomNumber</th><td class="roomsData">
eof

  foreach my $roomDate ( keys %{ $rooms{$roomNumber} }) {
foreach my $roomType (keys %{ $rooms{$roomNumber}{$roomDate} }) {


my $GuestName;
my $checkOutDate;
my$checkInDate;
chomp ($dateBeingChecked);
my $specificRoom = join "_", ($roomType, $roomNumber, $roomDate);
my $dat_file = "$Filepath$specificRoom";

open DAT, "< $dat_file" || die "couldnt open file\n";
	while ( my $Line = <DAT> ) {
		last if ( index($Line, ':') == -1 );
		chomp $Line;
		my ( $Keyword, $Value ) = split /:/, $Line, 2; #splits the line at the first : ignoring any others
		if ($Keyword eq "GuestName") {
			 $GuestName = $Value;
		} elsif ($Keyword eq "bookingReference") {
			$bookingReference = $Value;
		} elsif ($Keyword eq "CheckInDate"){
			$checkInDate = $Value;
		      #chop $checkInDate;
		} elsif ($Keyword eq "CheckOutDate") {
	            $checkOutDate = $Value;
			chop $checkOutDate;
		} else {
			warn "unknown field";
		}
	}


close DAT;
my ($yearBeingChecked, $monthBeingChecked, $dayBeingChecked) = split /\-/, $dateBeingChecked, 3;
my ($checkOutYear, $checkOutMonth, $checkOutDay) = split /\-/, $checkOutDate, 4;
my($roomYear,$roomMonth,$roomDay) = split /\-/, $roomDate, 3;
my ($checkINYear, $checkInMonth, $checkInDay ) = split /\-/, $checkInDate, 4;

#print <<EOF;
#checkInDate = $checkInDate<br />
#checkOutMonth = $checkOutMonth<br />
#roomMonth = $roomMonth<br />
#checkOutDay = $checkOutDay<br />
#roomDay = $roomDay<br />
#checkInDay = $checkInDay<br />
#EOF
if ($roomYear eq $yearOfCentury && $roomMonth eq $numericalMonthOfYear) { #only output for the month chosen
if ( $roomDate eq "$checkOutDate" ){ # determine the colour of the output 
print <<EOF;
<table class="specificRoomInfo"><tr><td class="checkingOut"> <a href="specificRoom/$specificRoom">$roomDay</a></td></tr></table>
EOF
} elsif ( $roomDay == "$checkInDay" ){
print <<EOF;
<table class="specificRoomInfo"><tr><td class="checkingIn"> <a href="specificRoom/$specificRoom">$roomDay</a></td></tr></table>
EOF
} elsif ($checkOutMonth >= $roomMonth && $checkInMonth <= $roomMonth && $checkOutDay > $roomDay  && $checkInDay < $roomDay){ # check for future dates
       
print <<EOF;
<table class="specificRoomInfo"><tr><td class="booked"><a href="specificRoom/$specificRoom">$roomDay</a></td></tr></table>
EOF

}else{
print <<EOF;
<table class="specificRoomInfo"><tr><td class="vacant"><a href="specificRoom/$specificRoom">$roomDay</a></td></tr></table>
EOF
}
}}
}
}
print "</td></tr></table><br /><br />\n";

} # end foreach my $roomNumber

} # end foreach my $roomType
} # end sub
 
That's not a subroutine, it's a whole program on its own...[smile]

Any possibility of seeing a sample of the input data? Maybe something's awry with the parsing.
 
the filenames are formatted like this
roomType_roomNumber_YYY-MM-DD
Double_101_2006-04-29
Single_102_2006-04-29
Twin_103_2006-04-29
Family_104_2006-04-29

Perlescent
 
you might be using the worng sort algorithm. From what I see you are just using "sort", which is the default perl sort which is not numeric. Maybe you should be using the numeric sort algorithm:

sort {$a <=> $b} <list to sort>
 
Kevin you're a star!! In my sort of the $roomDate I hadn;t entered your snippet. (actually I hadn't entered it anywhere). But when I added it to the:

foreach my $roomDate ( keys %{ $rooms{$roomNumber} }) {

to give

foreach my $roomDate ( sort keys %{ $rooms{$roomNumber} }) {

There it was.... perfect.

Thank you very much. I have been tearing my head out for a couple of days with that.

Perlescent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top