Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Bill Shelton and Adam Haskell and Marc Esher
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Bill Shelton Adam Haskell Marc Esher

Programmatically Completing A List Of Movie Showtimes In ColdFusion - Simplified

By
Published in Comments (4)

A couple of days ago, I came up with a solution for fleshing out a list of movie showtimes that only provided a partial set of AM/PM indicators. I wasn't terribly happy with the ColdFusion solution I came up with; however, it was the first solution that I had that actually seemed to work (I've been struggling with this problem for a while). In the comments to that post, Brian Rinaldi pointed out that the known AM/PM indicators were always on the last showtime of each portion of the day. Brian is clearly brilliant! And, his insight makes the problem orders of magnitude easier.

Once we know that the AM/PM showtimes are always on the last entry, fleshing out the list becomes a simple matter of looping over it backwards and applying every known AM/PM value to the unknown values that precede it.

<!--- Define our list of raw showtimes. --->
<cfset showtimesList = "11:00 11:40AM 12:20 1:00 1:40 2:20 3:00 3:40 4:20 5:00 5:40 7:10 7:50 8:30 9:50 10:30 11:10PM 12:00 12:40AM" />

<!--- Define our collection of fleshed-out showtimes. --->
<cfset showtimes = [] />

<!---
	Create a fail-safe default TT for our movie showtime
	parsing. This way, we have something to go on (even if it
	turns out to be wrong.
--->
<cfset currentTT = "PM" />


<!--- Convert the showtime list into an array. --->
<cfset showtimesArray = listToArray( showtimesList, " " ) />

<!---
	Loop over it backwards; the known AM/PM values are at the end
	of every portion of the day; as such, if we loop over the list
	backwards, we should alawys run into known AM/PM values that
	define the unknow listings before it.
--->
<cfloop
	index="index"
	from="#arrayLen( showtimesArray )#"
	to="1"
	step="-1">


	<!--- Get the current showtime parts. --->
	<cfset showtimeParts = reMatchNoCase(
		"\d+:\d+|AM|PM",
		showtimesArray[ index ]
		) />

	<!---
		Check to see if there is a known AM/PM value at this point.
		If so, then we want to start using it going forwrad (er, um,
		backwards).
	--->
	<cfif (arrayLen( showtimeParts ) eq 2)>

		<!--- Use the new TT value. --->
		<cfset currentTT = showtimeParts[ 2 ] />

	</cfif>

	<!--- Create the new showtime entry. --->
	<cfset arrayPrepend(
		showtimes,
		(showtimeParts[ 1 ] & currentTT)
		) />


</cfloop>


<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->


<!--- Output the original showtimes and our new showtimes. --->
<cfoutput>

	<strong>Original</strong>:

	#lcase( showtimesList )#

	<br />
	<br />

	<strong>Parsed</strong>:

	#lcase( arrayToList( showtimes, " " ) )#

</cfoutput>

As you can see, we keep track of whatever the last known AM/PM value is; then, as we loop backwards over the list, we always apply the most recently known value to the current showtime. Running the above code gives us the following output:

Original: 11:00 11:40am 12:20 1:00 1:40 2:20 3:00 3:40 4:20 5:00 5:40 7:10 7:50 8:30 9:50 10:30 11:10pm 12:00 12:40am

Parsed: 11:00am 11:40am 12:20pm 1:00pm 1:40pm 2:20pm 3:00pm 3:40pm 4:20pm 5:00pm 5:40pm 7:10pm 7:50pm 8:30pm 9:50pm 10:30pm 11:10pm 12:00am 12:40am

A huge thanks to Brian Rinaldi for seeing what I couldn't. His insights made solving this problem wicked simple.

Want to use code from this post? Check out the license.

Reader Comments

148 Comments

Wow . . . this is brilliant . . . kudos to both of you guys! Wish I could have more of these "brilliant" moments . . .

15,902 Comments

@Lola,

Yeah, Brian is the man. And, on the flip side, I get so irked when I don't see patterns that seem so obvious in retrospect :)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel