[Omaha.pm] Cool Hash Count Shortcut

Kenneth Thompson kthompson at omnihotels.com
Tue Aug 16 13:05:32 PDT 2005


Ok, so maybe this is old hat, but I found it incredible simple to
implement and even understand.

 

So here's the scenario... I have an object which contains all the
attributes of a reservation. I also have a list class which contains
many of my reservation objects. What I want is a count of how many of
each type of room I would need if all these reservations were to be
submitted for creation so I can reject them all up front before going
through the effort of running out of inventory half way through creating
them.

 

In SQL, it would be easy:

SELECT room_type, Count(*)

FROM reservations_to_be

GROUP BY room_type

 

Here's what I did given my existing structures:

 

     my %RoomTypes;

     $self->get_list->rewind;

     while (my $o_rl = $self->get_list->next) {

         $RoomTypes{$o_rl->get_room_type}++;

     }

 

So now, I can pre-check availability based on summary counts:

 

    If ($RoomTypes{"KN"} > [_some other availability number_]) {

        die "Not enough inventory available to fulfill request";

    }

 

The cool part about this is the key won't exist unless there is at least
one room type being requested in my batch or requests - it gets added
automagically as it's found. Plus, if I want to further break it down
Say, counts per type per day), I can just add more keys when adding it
up:

 

     my %RoomTypes;

     $self->get_list->rewind;

     while (my $o_rl = $self->get_list->next) {

       my $StartDate;

       my $EndDate = $o_rl->get_depart_date('obj');

       for ($StartDate = $o_rl->get_arrival_date('obj'); $StartDate <=
$EndDate; $StartDate += "1D" ) {

 
$RoomTypes{$StartDate->format("%Y-%m-%d")}{$o_rl->get_room_type}++;

       }

     }

    If ($RoomTypes{"2005-10-01"}{"KN"} > [_some other availability
number_]) {

        die "Not enough inventory available to fulfill request";

    }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/omaha-pm/attachments/20050816/df73eba3/attachment.html


More information about the Omaha-pm mailing list