Creating An ASCII Ruler In ColdFusion

Posted December 21, 2006 at 9:02 AM by Ben Nadel

Tags: ColdFusion

I posted this as a solution to CF-Talk the other day and I thought I would post it here as well (for some reason, not everyone and their mother subscribes to CF-Talk??). Paul Vernon wanted to create an on-screen ASCII ruler that would fully display the powers of 10 (10, 20, 30... 150, 160, etc) but for each non-power-of-10, only display the ones digit (1,2,3,4,5,67,8,9).

My solution builds the output into a two dimensional array. I was treating each character in the output as a sepparate index in the array. Places that has no digit were given a white space character. Then the array is converted to a string using a Java StringBuffer.

I am not 100% sure what this would ever be used for, but here goes:

  • <!--- Get the length of the ASCII ruler. --->
  • <cfset intRulerLength = 150 />
  •  
  • <!---
  • Get the ruler height. This is the number of digits that
  • the max value will have. Since the rule will count up from
  • 1, the max height will be determined by the last value in
  • the ruler (ie. the length of the ruler).
  • --->
  • <cfset intRulerHeight = Len( intRulerLength ) />
  •  
  • <!---
  • Create the array in which to build the output. This is a
  • two-dimensional array. The first index is going to create
  • the ROW of the output (digit column). The Second index is
  • going to be the COLUMN of the output (the index on the ruler.
  • --->
  • <cfset arrRuler = ArrayNew( 2 ) />
  •  
  •  
  • <!---
  • Now, let's start building the ruler output by looping from
  • 1 to the length of the ruler.
  • --->
  • <cfloop index="intColumn" from="1" to="#intRulerLength#">
  •  
  • <!---
  • Check to see if we are at a power of 10 (MOD will not
  • return a remainder). If we are, then we need to display
  • all digits, otherwise, we are just going to display the
  • ones digit column.
  • --->
  • <cfif (intColumn MOD 10)>
  •  
  • <!--- Fill the entire column with spaces. --->
  • <cfloop index="intDigit" from="1" to="#intRulerHeight#">
  •  
  • <cfset arrRuler[ intDigit ][ intColumn ] = " " />
  •  
  • </cfloop>
  •  
  • <!---
  • Now that we have filled the column with spaces, put
  • the ones digit in the last space.
  • --->
  • <cfset arrRuler[ intRulerHeight ][ intColumn ] = (intColumn MOD 10) />
  •  
  • <cfelse>
  •  
  • <!---
  • We need to output the entire value in the output.
  • But, this value might not be the same length as the
  • max value length. Therefore, we need to
  • right-justify the value to fit exactly with the
  • ruler's height.
  • --->
  • <cfset strValue = RJustify(
  • intColumn,
  • intRulerHeight
  • ) />
  •  
  • <!---
  • Now loop over the value and apply to the ruler
  • output array.
  • --->
  • <cfloop index="intDigit" from="1" to="#intRulerHeight#">
  •  
  • <cfset arrRuler[ intDigit ][ intColumn ] = Mid(
  • strValue,
  • intDigit,
  • 1
  • ) />
  •  
  • </cfloop>
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  • <!---
  • ASSERT: At this point, the entire output for the ASCII ruler
  • should be stored in the ruler array. Now, we need to
  • translate that into a horizontal ruler. To make this faster
  • and control the white space, let's create a Java string
  • buffer to which we can systematically append output data.
  • --->
  • <cfset sbOutput = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).Init() />
  •  
  •  
  • <!---
  • Loop over the ruler array and append to output. Since we
  • need to output the ruler horizontally, we need to do each
  • row at a time.
  • --->
  • <cfloop index="intDigit" from="1" to="#intRulerHeight#">
  •  
  • <!--- For each row of digits, loop over every column. --->
  • <cfloop index="intColumn" from="1" to="#intRulerLength#">
  •  
  • <!---
  • When appending, we need to convert the array data
  • to a string to make sure Java don't choke.
  • --->
  • <cfset sbOutput.Append(
  • JavaCast(
  • "string",
  • arrRuler[ intDigit ][ intColumn ]
  • )
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- After each row, add a line break. --->
  • <cfset sbOutput.Append(
  • JavaCast(
  • "string",
  • Chr( 13 ) & Chr( 10 )
  • )
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- Output the ASCII data in PRE tags for best formatting. --->
  • <pre>#sbOutput.ToString()#</pre>

This gives us output that looks like this:


 
 
 

 
ASCII Ruler In ColdFusion MX 7  
 
 
 

So, not sure what this is used for, but that's one solution.



Reader Comments

There are no comments posted for this web log entry.

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 »