Category Archives: programming

MySQL Capitalize Function

I wrote a little MySQL function to capitalize the first letter of every word in a string. I thought I’d share since I wasn’t able to google for it.

CREATE FUNCTION CAP_FIRST (input VARCHAR(255))

RETURNS VARCHAR(255)

DETERMINISTIC

BEGIN
	DECLARE len INT;
	DECLARE i INT;

	SET len   = CHAR_LENGTH(input);
	SET input = LOWER(input);
	SET i = 0;

	WHILE (i < len) DO
		IF (MID(input,i,1) = ' ' OR i = 0) THEN
			IF (i < len) THEN
				SET input = CONCAT(
					LEFT(input,i),
					UPPER(MID(input,i + 1,1)),
					RIGHT(input,len - i - 1)
				);
			END IF;
		END IF;
		SET i = i + 1;
	END WHILE;

	RETURN input;
END;

So running the following code...

SELECT CAP_FIRST(
	'this is totally like   @ TEST 1 right!' 
)

Returns the string "This Is Totally Like @ Test 1 Right!"

I would rather have regex'd it, but I couldn't find any sort of regex replace function in the docs.

Stack Overflow and Ruby Regex

I’ve been following the Stack Overflow podcast for a little while and I’ve been itching to come up with a question to ask on the site. Well, today I was working on a little ruby formating script (more on this later) and I wanted to replace all the spaces in a string that were NOT located between quotes.

I asked my question on the site and a few hours later I had some really great working solutions!

Click the link for solutions and definitely check out http://stackoverflow.com!

JavaScript Game Of Life Pt 2

I changed up the code up a little bit for implementation of Conway’s Game Of Life. Turns out I had misread the rules a bit. My original code turned all cells on when they had 2 or 3 neighbors, regardless of their current state. That’s how live cells are supposed to work, but dead cells are only supposed to change their state when they have exactly 3 neighbors.

Also I’ve updated my JavaScript objects to use the Prototype Class.create method. As far as I know the method I had been using works in all browsers, but I like Prototypes implementation of constructors and inheritance.

Finally, I increased the number of live starting cells, shrunk the number of boxes and increased the time delay between frames to hopefully grant a better user experience. It probably still looks terrible in Internet Explorer. Once again, that’s what you get.

So check it out and download the code!

PS: You can still download and view the original version if you like.

JavaScript Game Of Life Pt 1

I thought it would be fun to implement Conway’s Game Of Life. I used ColdFusion a bit in my example to ease some typing but the “engine” is all JavaScript. Beloved Prototype is also being used a bit behind the scenes.

So without further ado, here’s a

// set up a game of size 30 x 30

g = new Game_Of_Life(30);

// set some random tiles, note that you
// have way more of these for a size
// 30 board.

g.set_tile(2,26,1);
g.set_tile(19,20,1);

// draw inital board

g.draw();

// run the game, 1/25s "frame-rate" 
g.run(.25);

Click to see a demonstration.

And, as always: Download the code!

PS: It looks like terrible in Internet Explorer. No comment.

ColdFusion NPR API Pt. 2

I’ve dug a little deeper into the NPR API. I’ve added 3 new components. Feed.cfc represents the data returned from the api call. Story.cfc models the actual stories that make up the feed.

The 3rd object is a simple object that I extend to share some common methods to between my other components. I generally consider this bad practice and there are a lot of tools designed specifically to address this problem.

I did it for a couple of reasons. First, I wanted this to self-contained and stand alone. I wanted it to be as easy as possible to drop it into whatever project YOU have, with whatever tools YOU have set up and get it going. Second, I wanted to focus more on the real meat of the product without getting too hung up on the implementation details. Besides, I can always refactor!

Stay Tuned!

Example Usage:


 







	

Download the code!

Sarah Palin Response Generator

I always get jealous whenever I see one of those mildly funny internet random text generators. I’ve finally done something about it. Now hopefully it’ll be (even more) trivial for me to make one of these in the future.

For the time being I’ve filled it up with some juicy Sarah Palin quotes. The question area is irrelevant. Apt.

Sarah Palin Response Generator

and as always…download the code!

ColdFusion NPR API Pt. 1

NPR published an API a while back giving easy access to 250,000+ NPR stories.

As I’m still quite the newb when it comes to Object-Oriented Programming I thought I would blog through the process of creating an OO ColdFusion wrapper.

Example Usage:










Download the code!

PS: In order to actually run this you’ll need to register for an account and API key. Also, browse the aggregate to ID mapping info to find yourself some id’s to use!

PPS: A much-love shout-out to my local NPR affiliate WMFE. Support publc radio!