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,246 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,246 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 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools