Merge sort in Coldfusion

For far too long I've been using Bubble sorts, they're very basic and very slow but mainly I've been sorting tiny little arrays or lists and the server just don't care about it.

But that's not the proper way of doing things. Now I don't really understand sorting algorithms all that much so I use this site to work out which one I want to use, plus a little bit of wiki:

Sorting-algorithms.com

I've decided to use merge sort from now on mainly because it's quite fast and although it generally seems to be a tiny bit slower than a shell or heap sort, it is stable which shell and heap are not. I don't really know what that means but it sounds safe! Also it works much better on large sets than any of the simple algorithms.

The algorithm is more complex than a Bubble sort and although I understand the gist of it I decided to find someone elses code and use that. So I did.

Simon Horwith's blog entry on search algorithms

This guy has made a fantastic little demo app, very comprehensive, with 6 different sortin algorithms in it. You can download the zip and drop it into a server and test the speeds of each algorithm. Then the code is there for you to play with, or as Simon put it: I look forward to hearing how people put them to use as well as any other findings with them.

I've tweaked his code a bit, so it will take an array of structures/objects, and sort by any publically accessible field of the that structure. Here's how I've used it in the test page:

view plain print about
1<cfscript>    
2        sort = new mergesort();
3        
4        a = {};
5        a.name = 'Pete';
6        a.age = 27;
7        
8        b = {};
9        b.name = 'Holly';
10        b.age = 22;
11        
12        c = {};
13        c.name = 'Jesus';
14        c.age = 2012;
15        
16        peeps = [a,b,c];
17        
18        writeoutput('<h1>Original array</h1>');
19        writedump(peeps);
20        
21        // sort peeps array, by oject.name into Descending (default) order
22
        peeps = sort.sortArrayByFieldAscending(peeps,'name');
23        
24        writeoutput('<h1>Sorted by name in descending (default) order</h1>');
25        writedump(peeps);
26        
27        // sort peeps array, by oject.age into Ascencing order
28
        peeps = sort.sortArrayByFieldAscending(peeps,'age',true);
29        
30        writeoutput('<h1>Sorted by age in ascending order</h1>');
31        writedump(peeps);
32        
33        
</cfscript>

You can view the live demo of it working here http://skeater.co.uk/samples/mergesort.cfm and I've also attached a .zip of the .cfm and .cfc.

Thanks to Simon Horwith, and hopefully someone else will find this useful!

Precisely the same Railo mistake as last time

In the past I've had problems with my blog working on mobiles. Something to do with the way it's set up for Coldfusion but I'm running Railo.

I blogged about it here: My very few BlogCFC woes

So when I switched across to my new server recently, I forgot to make the changes to get it running on my new server. I have in fact done something slightly differently this time though, I've changed the /railo/tomcat/conf/web.xml file instead of the one in the instance of Skeater.co.uk, so the changes are global.

view plain print about
1<!-- Mappings for the Railo servlet -->
2 <servlet-mapping>
3 <servlet-name>GlobalCFMLServlet</servlet-name>
4 <url-pattern>*.cfm</url-pattern>
5 <url-pattern>*.cfml</url-pattern>
6 <url-pattern>*.cfc</url-pattern>
7 <!-- Basic SES Mappings -->
8 <url-pattern>/index.cfm/ *</url-pattern>
9 <url-pattern>/default.cfm/ *</url-pattern>
10 <url-pattern>/post.cfm/ *</url-pattern>
11 <url-pattern>/archive.cfm/ *</url-pattern>
12 <url-pattern>/blog.cfm/ *</url-pattern>
13 <url-pattern>/page.cfm/ *</url-pattern>
14 <!--- NEXT LINE FIXES THE MOBILE SITE SES URLS --->
15 <url-pattern>/mobile/index.cfm/ *</url-pattern>
16 <url-pattern>/rewrite.cfm/ *</url-pattern>
17 </servlet-mapping>

Note: once again, the white space between the slash, /, and the star, *, is just to stop the blog trying to use it as a comment.

So thanks Sparky for pointing out the error, and no thanks to you Pete, for being an idiot and not correcting the problem that you've had before so you should have known! Dumb ass.

Editing all the links in a page

I'm not sure if this is a duplicate post of not... I couldn't find the original one, so here goes anyway.

We had the situation at work where we wanted to find all the links in a news article, in the back end editing system, and send them all to something like bit.ly to shorten them, so make it less likely that the article hit the maximum word count.

This might have been a forum post actually, anyway, that's besides the point.

So I came up with a quick example to work out how to do it. Instead of using a URL shortening API for the demo I've just reversed the hrefs. But it would be easy to swap them that code out for bit.ly or something similar.

view plain print about
1<!DOCTYPE html>
2<html>
3 <head>
4 <title>
5 Edit HREF's demo
6 </title>
7 </head>
8 <body>
9 <cfsavecontent variable="longText">
10 This is totally copied from <a href="http://www.bennadel.com/blog/2307-Disabling-Auto-Correct-And-Auto-Capitalize-Features-On-iPhone-Inputs.htm">Ben Nadel's blog</a>, an idea which he got in turn from <a href="http://www.abookapart.com/products/mobile-first">A Book Apart: Mobile First</a>, but I thought it was worth mentioning again.
11 </cfsavecontent>
12    <cfoutput>
13    <!--- output original text --->
14        #longText#
15    </cfoutput>
16
17 <cfscript>
18        // find the a tags
19
        arrHref = reMatchNoCase('<a href="[^"]*"[^>
]*>
',longText);
20
21        // get the array length and loop over
22
cntHref = arrayLen(arrHref);
23        for(h = 1; h LTE cntHref; h++) {
24        
25            // get the individual href,
26
            thisHref = arrHref[h];
27            thisHref = reReplaceNoCase(thisHref,'
<a href="([^"]*)">','\1');
28
29            // reverse it, bit.ly it, whatever takes you're fancy
30            reverseHref = reverse(thisHref);
31        
32            // replace the original href with the new one
33            longText = replaceNoCase(longText,thisHref,reverseHref);
34        }
35 </cfscript>
36 <hr />
37 <cfoutput>
38        <!--- output edited text --->
39 #longText#
40 </cfoutput>
41 </body>
42</html>

You can see the live sample here: http://skeater.co.uk/samples/hrefEdit.cfm

It might not be completely robust, it might need updating for production use but the concept is there and it works.

Simple passwordless SSH tutorial

It took me many attempts to work out password-less ssh, mainly because a lot of internet guides are not written that well and following them step by step didn't work.

Then I found one that did work! Woohoo!

So I've copied and pasted it here... and regrettably forgotten the link I found it from. If anyone recognizes this then I'll gladly reference it to the correct source.

The guide makes use of local$ and remote$, this is to indicate if you are typing on your local or remote server.

Create the private key

view plain print about
1local$ ssh-keygen -t rsa (accept default file locations and create a password for your private key)
2local$ scp ~/.ssh/id_rsa.pub user@remote:/home/user/
3local$ ssh user@remote (enter normal root password)
4remote$ cat ~/id_rsa.pub >
> ~/.ssh/authorized_keys
5remote$ chmod 600 ~/.ssh/authorized_keys
6remote$ exit
7local$ ssh user@remote (at this point you shold be asked to enter the password for your private key)

Add private key password

The password for the private key needs storing in your machine. This is encrypted.
view plain print about
1local$ ssh-agent bash
2local$ ssh-add ~/.ssh/id_rsa
3local$ ssh user@remote (you should no longer be asked for a password)

Note: in the code examples above, don't type the stuff in brackets

Note 2: I think this was the blog I got the entry from, but it doesn't work any more so I can't be sure... I definitely looked there before I found it anyway: http://blogs.translucentcode.org/mick/archives/000230.html

Scotch on the Rocks is on!

If I had to guess, I'd say there were about 5 people that read this blog that make websites and probably only 1 that do Coldfusion coding (and Tom already knows about SOTR).

But just in case there is someone else that find this blog and does Coldfusion, I'd like to point out that SCOTCH ON THE ROCKS 2012 IS GOING AHEAD!

I've only been to one SOTR before (2011), I found out about it just after the 2010 event and was very gutted, as many amazing Coldfusion developers and other internet/web/computer/geek type celebrities were going to be there as well.

Anyway I managed to get along to the 2011 event and found it incredibly useful. I've actually changed a huge amount of my working style since going there, as I learnt about new technologies and gathered advice from other developers.

I also got to see about 5 seconds of porn...due to a slight error of one of the presenters.

Breasts aside though, it's an excellent conference, sponsored last year by Adobe themselves, as well as Railo (free open source Coldfusion server), and hosted/organised mostly by Fuzzy Orange.

Last year there were talks on HTML 5, CSS 3, Regex, various database technologies, increasing your efficiency as a developer and many many other things. So even if you don't do Coldfusion it's worth going, because there are other beneficial talks, it's backed by Adobe, and it'll help teach you how incorrect you are by not using Coldfusion (or Railo).

So basically, go to Scotch on the Rocks 2012.

There are no details about it yet... it might be in Edinburgh, it might be in Amsterdam. Either way I'm going, surely that's reason enough for you to go?

Disable iPhone auto capitalize/correct on web pages

This is totally copied from Ben Nadel's blog, an idea which he got in turn from A Book Apart: Mobile First, but I thought it was worth mentioning again.

You can disable the auto correct/capitilize features on form inputs on the iPhone, so when users visit your site and fill in the forms the iPhone wont try to correct the spelling of you email or silly username.

It's quite simple to do:

view plain print about
1<textarea rows="2" cols="20" autocapitalize="off"></textarea>
2<input type="text" autocorrect="off" autocapitalize="off">

So you just use the autocapitalize and autocorrect properties.

It's designed for the iPhone and Safari, so I don't know how far it goes but it'd be a cool feature to see on other browsers and platforms as well.

The example code is from the Apple Developer Library

Screens on Linux

I've recently started using screens when inside command line linux.

I got shown it by some Rackspace tech guys as I wanted to leave a process running after I'd left me shell.

Screens is brilliant, it lets you run multiple shells and allows you to switch between them with keyboard shortcuts, and you can close and reopen your ssh client (like putty) and all your "screens" are still there, so you can reconnect to them... or "attach" as screens calls it.

It's like having multiple terminal/putty windows open, except when you close the actual GUI window, the shell is still running.

What I've found this useful for is being logged into multiple GIT repositories and also having a couple of extra shell "screens" for database and server admin.

Anyway, this post is a little but rushed as I'm on my lunch and I've still not finished my super noodles, but use screens if you don't already and you use a command line linux server.

And I've put up a Linux section under my code reference, with a screens page in there...

Or I will do shortly... I'll link to the page in here once I've made it!

CSS3 box-sizing! Why didn't anyone tell me!

When something website development based annoys me, I usually just write some sort of hack or fix for it, and do that over and over again until I really get sick of it before I come up with a proper solution.

That point in time arrived today, with respect to the issue of setting a single width to all inputs and textareas in a form, and watching the browser render them all with different widths! It happens and it's stupid.

"What in shitting crikey are you talking about?!" you may be shouting at the screen. Well I'll show you, check dis code:

Some css setting input and text area widths, stacked using block labels

view plain print about
1label {display:block;margin:1em}
2input, textarea {
3 width:100%;
4}

A simple form with a text input, submit button and textarea

view plain print about
1<form>
2 <label>
3 <input type="text" value="Text box">
4 </label>
5 <label>
6 <input type="submit" value="Submit button">
7 </label>
8 <label>
9 <textarea>This is a text area</textarea>
10 </label>
11</form>

I've added the red line in to show you, the form elements don't line up.

This situation can only be described as a shitting bastard

In the past, I've just done stupid things like set the submit button to 100% and the text area to 95% and the text input to 98% or used 3 different exact pixel widths. I've been a moron about it but it's got me through.

I got sick of doing that today... so I just googled the problem and almost immediately found the answer.

box-sizing

This is a new CSS3 rule, but it works on IE8+, and recent versions of Firefox, Chrome, Safari and Opera. It's a good rule, and anyone using IE7 and IE6, shouldn't be. And you'll get wonky forms.

But good people, will see nice neat form edges! AND THAT MATTERS TO ME! Look, by changing the CSS slightly:

view plain print about
1label {display:block;margin:1em}
2input, textarea {
3 width:100%;
4 -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
5 -moz-box-sizing: border-box; /* Firefox, other Gecko */
6 box-sizing: border-box; /* Opera/IE 8+ */
7}

Holy muntpants batman it works! Look at the lovely neat lined up inputs! Yes, the button is maybe a pixel off, but you can't really see that without that red line there.

Ok, ok there is some vendor specific guff in there, but to be honest that might have gone by now... I stole that code from a year old blog post. Oh and it really does work on IE8.

So I've learnt a couple of lessons from this experience.

  1. If somethings naff, try to fix it straight away instead of hacking around it for years
  2. Read more release notes... this CSS3 property has been around for ages

So there you have it, CSS3 box-sizing. Wonderful.

You can have a play here using jsFiddle, which is a wonderful site: box-sizing test

I should also give credit to the guy's blog post that I found this on and also the official W3C documentation on it

Mac dilema

I used to hate Macs quite a lot. I really did.

Then I decided I just didn't like them, I'm a windows guy, but everyone else is entitled to use the incorrect operating system.

Then I decided each to his own, and my own was Windows 7. But I wanted an iPad because they're really cool.

Then I started writing iPhone apps, so I had to get a Mac. I borrowed a Core 2 Duo Mac Mini... Which is pretty naff, at browsing the internet, let alone developing apps. It's slow and stupid and annoying... But beggars can't be choosers I suppose.

The Mini might have struggled because I had xcode running, but that's no excuse, I'm never actually using xcode when Safari takes 5 minutes to open a new tab.

Anyway, I also use to hate iPhones, I loved my Nokia N900 because it ran Linux and it's awesome and THE KEYBOARD IS SOOOOOOO GOOOD! But it wasn't really compatible with anything. The twitter and facebook apps where naff, and it didn't do Dropbox or Evernote.

There were some In development apps for it but it was all a bit open source for me. So I got my iPhone, and I do like it now. It's just a mini iPad, but I still hate the stupid touch screen keyboard. The iPad has nice big buttons it's fine, the iPhone has tiddly little idiot buttons for mice. I'm not a mouse.

When I can afford it I'll probably get an Android with a keyboard and just relegate the buttonless moron to testing. Even though the UI is lovely and pleasant.

But that's all besides the point, that's just the story of how I got here. I'm going to buy myself a Mac. I'll keep my wnderful home made Wn 7 PC for games and graphics and websites, but I need a Mac for iOS coding, and I could do with a laptop for working on the go.

So do I buy the cheapest Mac laptop, the 11inch Air for £850 but only 2GB of RAM, or the 13 inch 4GB Air for £1,099 or the 13 inch 4GB Pro for £999.

They all have i5 processors... None of them have a proper graphics card. You have to spend 15 hundred for that... so no games on it. But it is a work machine after all.

So can the slower i5 processors in the Air handle xcode? Or does their solid state HDD give them the upper edge? Storage space doesn't matter that much to me, but the 64gb in the 11inch air does seem a bit small.

Anyone got any thoughts or experiences on the Air/Pro dilema?

Chrome beats Firefox hands down

I've been using Chrome as my main browser for a while now, but I've always had to nip in and out of Firefox to use Firebug.
I don't really like Opera's dragon fly, some say it's great but it's too different for me to bother learning. The web tools in Safari and Chrome have some good features, but don't (well didn't) beat Firebug. And IE... Well, it's IE. so it is just worse all round. So much worse.
Chrome has had a pretty big update now though and they've added some cool features to the developer tools that make it more on a par with Firebug.
It's much easier to edit styles on an element in the page, FB was always good at this and Chrome made it into an awkward and annoying hassle, but that's been fixed. They've also introduced box model highlighting on the page, that activates when you inspect an element in the DOM browser. It also shows the pixel size whilst highlighting the element. I would say this feature beats FB's equivalent.
The Chrome toolbar also features tue Javascript debugger, like FB again, and they are almost the same in functionality but Chrome seems to just have a few more features, like "contiune to here" which FB doesn't seem to have.
I still think the UI in Chrome is better than Firefox, it's cleaner and easier to customize, and Firefox is slow and clunky, even without plug ins. Firefox needs lots of plugins to make it useful, Chrome has more features built in. These extra plugins in Firefox slow it down far too much, and without them it's nomise - Firebug is one of these plugins.
Chrome Apps are nice too, although alot of them are pointless, silly and rubbish... You can get Angry Birds as a Chrome App, now that's awesome.
Finally, to make Chrome completly amazing I did have to add a few of plugins for it. One was awesome screenshot, which is a really easy way to take screen shots of pages you're on and save them as files or upload them to certain web services. Another one is the evernote app, which lets me "clip" web pages and text to my evernote account, and finally I installed Speed Dial 2 which replaces the Chrome Home screen.
The Chrome homescreen is pretty bad, and Google keep updating it and changing the way I've got ot set up. Speed Dial 2 lets you add links to your favourite web pages and apps, add a link while you're browsing the site, rearrange the links on your home screen, change the picture for the link or let you use the view from the web page, as well as many many other things. It really is very useful.

Anyway, I've always loved Chrome and now I love it even more. Those wonderful apps/plugins I was talking about are

More Entries