Saturday, May 30, 2009

Dynamic Arrays

I have to shamefully admit... I didn't prepare anything for this week. I'd hate to post nothing, so instead I'll post something which I don't consider completely finished yet, but it's far enough along that it probably works just fine.


Working with arrays in classic ASP is frustrating. I wanted to make it more like working with listbox controls in Visual Basic. Note that I wrote this class before I started learning ASP.NET; I wanted to revamp it to bring it in line with the ArrayList class in VB.NET, but just haven't had time yet. More importantly, I haven't really had reason. All my current projects are in PHP or ASP.NET; I've essentially abandoned classic ASP. But I know there are people out there with classic ASP applications which they can't completely rewrite, so I intend to continue writing classic ASP functions.


Considering the enormous size of this class, there's no way I'm going to attempt posting it here, but it'll be on Snipplr as usual. Count, Item, and Items are implemented as properties. Subroutines and functions include: Remove, RemoveAll, Add, BinarySearch, Exists, Random, Reverse, Sort (calls QuickSort; also available are BubbleSort, CombSort, ExchangeSort, and SelectionSort), Shuffle, Swap, Sum, and Product.


View ASP implementation on Snipplr

Saturday, May 23, 2009

Prime Numbers

Once upon a time I wrote some functions for finding and checking prime numbers. In this library of functions you'll find the following:


  • getPrimes() - finds prime numbers up to a specified limit
  • isPrime() - checks if a number is prime using modular division against odd numbers
  • isPrime2() - checks if a number is prime using modular division against known primes
  • primeCount() - counts the number of primes less than or equal to the specified number
  • isComposite() - the opposite of prime
  • isPrimeSpeedTest() - races isPrime againts isPrime2

In theory, checking against known primes should be faster than checking against all odd numbers, but it turned out to be slower because I was first finding the primes with getPrimes(). If a list of prime numbers was hardcoded in an array it might be faster for smaller numbers, and then the odd number method could be used for larger numbers.


View ASP implementation on Snipplr

Saturday, May 16, 2009

Chemistry Library

This week's code is probably the biggest single release I've ever made. I considered splitting it up into two or three weeks, but that would be too much work. Before we get started, you'll need my proper case function.


In this function library, we have three major things going on: look up chemical element symbols by atomic number, look up chemical element names by atomic number, and figure out the electron configuration of an atom given the atomic number.


For the chemical element names and symbols, I made an array of all the named elements; currently the highest named element is atomic number 111. There are elements with higher atomic numbers that have been discovered, but not yet named. There is also the possibility that more elements may be discovered in the future. To handle both these situations, I wrote code to generate standardized names if the array lookup fails.


The function(s) for figuring out electron configuration are my favorite. I started with an array of all the subshells in the order they get filled. Then I wrote a function that knows how many electrons can fit in each type of subshell. Finally, I wrote the main function which fills the subshells, taking into account known exceptions to the rules. (e.g. palladium)


Saturday, May 9, 2009

Fuel consumption

Last November I was at a Service Canada office and picked up a pamphlet called Fuel Consumption Guide 2007. Although too old to be relevant (unless you're buying a used car), it contained some formulae for calculating fuel consumption which inspired me to write some code.


This library of functions is among the longest yet, so I won't be posting the code directly on the blog, but here's a list of the functions included:


  • lph() - Calculate fuel consumption rating in litres per 100 kilometres.
  • mpg() - Calculate fuel consumption rating in miles per gallon.
  • lph2mpg() - Convert miles per (imperial) gallon to litres per 100 kilometres.
  • mpg2lph() - Convert litres per 100 kilometres to miles per (imperial) gallon.
  • fuelConsumption() - Calculate fuel consumption in litres.
  • CO2emissions() - Calculate carbon dioxide emissions in kilograms.

Saturday, May 2, 2009

Regular Expressions

Over the past year, I've posted a lot of code that made use of regular expressions. When used appropriately, they can be very powerful. But regular expressions in ASP are a little more cumbersome than PHP. Wouldn't it be great if ASP had the simplicity of regular expression functions?


Once again, the source code is too long to post here, so it will only be available on Snipplr. This library of functions includes the following:

  • ereg() - case-sensitive regular expression match
  • eregi() - case-insensitive regular expression match
  • ereg_replace() - case-sensitive regular expression replacement
  • eregi_replace() - case-insensitive regular expression replacement
  • sql_regcase() - make regular expression for case insensitive match

View ASP implementation on Snipplr