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 »
10,743 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 »
10,743 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »