#!/usr/bin/perl

# Find times of the form NN:NN (am/pm) and
# return them in delimited list.

$delimiter = "--";

$line = <stdin>;
$done = 0;

$times = "";


while (defined($line))
    {
    chomp($line);
    while (length($line)>0)
	{
	$found = 0;
	$ampm = "";
    	$line = $line."xxx";

        # print("Checking:  $line \n----------\n");

	$matchstr1 = "^(.*?)([0-9][0-9]?):([0-9][0-9])(:[0-9][0-9])?\\s?(a|A|p|P)?(.*)\$";

	if (($line) =~ /$matchstr1/i)
		{
		$found = 1;
		$hour = $2;
		if (length($hour)==1)
			{
			$hour = "0".$hour;
			}
		$min = $3;
		$ap = $5;
		$remainder = $1."XX".$6;
		}

	if ($found == 0)
		{
		$matchstr2 = "^(.*?)([0-9][0-9]?)(am|pm)(.*)\$";
		if ($line =~ /$matchstr2/i)
			{
			$found = 1;
			$hour = $2;
			$min = "00";
			$ap = $3;
			$remainder = $1."XX".$4;
			}
		}

	if ($found ==1)
		{
		# print("line = <$line> \n");
		# print("Found!  hour=$2, min=$3, sec = $4, a/p=$5 \n");
		# print("Remainder = <$remainder>\n");


		if ($ap =~ /^a/i) 
			{
			$ampm = "am";
			}
		if ($ap =~ /^p/i) 
			{
			$ampm = "pm";
			}

		$wholetime = $hour.":".$min.$ampm;
		# print("<$wholetime>\n");

		if (length($ampm)<2)
			{
			# Add both am and pm!
			push (@times, $wholetime."am");
			push (@times, $wholetime."pm");
			}
		else
			{
			push (@times, $wholetime);
			}
		}
	else
		{
		$matchstr1 = "^(.*?)(N|n)oon(.*)\$";

		if (($line) =~ /$matchstr1/i)
			{
			push (@times, "12:00pm");
			$remainder = $3;
			$found = 1;
			}
		}

	if ($found ==1)
		{
		$line = $remainder;
		}
	else
		{
		$line = "";
		}
	}
    $line = <stdin>;
    }


@times = RemoveDuplicates(@times);

# $len = scalar(@times);
# print("\n----There are $len times. -----------\n\n");

@times = (sort @times);

while (scalar(@times))
	{
	$thistime = shift(@times);
	if ($thistime =~ /0?(.*)$/ )
		{
		print("$1");
		}
	if (scalar(@times)>0)
		{
		print("$delimiter");
		}
	}




sub RemoveDuplicates
#
#       Removes any elements that occur twice or more in an array.
#

        {
        my (@arr) = @_;
        my ($arrsize) = scalar(@arr);

        my ($notdone) = 1;

        while ($notdone)
            {
            $notdone = 0;
            for ($count1 = 0; $count1 < $arrsize; $count1++)
                {
                for ($count2 = $count1+1; $count2 < $arrsize; $count2++)
                        {
                        if ( $arr[$count1] eq $arr[$count2] )
                                {
                                splice(@arr,$count2,1);
                                $count2--;
                                $arrsize--;
                                $notdone=1;
                                }
                        }
                }
            }
        return (@arr);
        }


