What you describe attempting to do with your script should work but there are more efficient ways to do that.
If you sort your client records first by the month of their birthday and then by their gender, you can set up a layout based on the client function where you have removed the body layout part and added two sub summary layout parts. The first would have a "when sorted by Month" setting. (You may have to add a field that returns the Month number if you don't have one already) and the second would be "When Sorted by Gender". If you put a summary field that is a "count of" summary field to count your records inside this second sub summary layout part, you will be able to sort by Birthday month and by Gender to get your counts for each month broken down by Gender.