Programmatically Completing A List Of Movie Showtimes In ColdFusion - Simplified

Posted May 4, 2011 at 10:54 AM by Ben Nadel

Tags: ColdFusion

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.




Reader Comments

May 4, 2011 at 11:19 AM // reply »
148 Comments

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


May 4, 2011 at 11:47 AM // reply »
11,238 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 :)


May 5, 2011 at 5:14 AM // reply »
2 Comments

Gr8...Brilliant code in simple way...


May 9, 2011 at 11:00 PM // reply »
11,238 Comments

@Ajo,

Thanks :)


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools