Archive for July, 2006

Mastering Regular Expressions

by Jeffrey Friedl

Regular expressions are the most useful tool in string manipulation going - but learning about them is a real pain. Essentially, they’re confusing for most folks because of an apparently opaque syntax, and because there are so many different ‘flavours’ of them.This book does more than anything else I’ve read to make all this clearer.

It’s a very ‘first principles’ book, looking at how regular expressions are processed, down to how they work through a string, character by character. This is useful, as it makes you realise how they work.

Later, it goes through more advanced features (lookaheads, etc.), and then it examines some of the most commonly used regular expression syntaxes - Java, .NET and Perl. It’s also got a lot of examples and good ideas on writing efficient expressions for the sort of stuff you do day to day - matching email, parsing arguments from the command line, that sort of thing.

At work, I’m our ‘regular expressions’ guy now, ‘cos I read this book, and I use it as a reference on a frequent basis. I also use regular expressions LOTS in my programming - and by God is it faster than writing code to process strings.

My only complaint - well, I wouldn’t mind a few wee examples in other languages - Javascript being the obvious one, and PHP and Ruby. They’re all VERY similar to Perl, so it wouldn’t take long to highlight any differences and explain how to use them.

However, it is a great book for the subject - just look at it on Amazon. My opnion? Every coder should read this book (or one just like it). Programming without Regexes is just crazy.

XML Data Islands

I never knew you could do this - although quite when it’d be useful escapes me - XML and HTML being merged on the browser

Error Starting Workflow when a new Sharepoint Item is created

So, as I mentioned in an earlier post, I was having some problems creating a workflow which I wanted to initiate when a new item was added to a forms list. Well, I think I solved it. Read more »

More about frozen panes…

So, I’ve been working with ‘Frozen’ panes in tables in HTML. The problem is, some of these tables are, well, a little big. Like maybe 100 cells square. I found that the technique mentioned earlier in my blog didn’t work very well, as the scrolling on the DIV tag became slow and jerky.

This makes sense really - each cell is having it’s CSS rerun each time. Then it struck me - the styles were defined as:

td.frozen {
padding: 3px;
position:relative;
top: expression(document.getElementById('pane').scrollTop-2); /*IE5+ only*/
z-index: 5;
}

This mean that ‘getElementById’ was being run repeatedly. However, the style’s JavaScript was being run before ‘onload’. I just couldn’t run the ‘getElementById’ to populate a global variable after the element had been created, but before the style expressions were run. Instead, in a moment of clarity, I changed the style to:

td.frozen {
padding: 3px;
position:relative;
top: expression(getPane().scrollTop-2); /*IE5+ only*/
z-index: 5;
}

And added a script:

var pane;

function getPane() {
if( pane == null ) {
pane = document.getElementById(”pane”);
}
return pane;
}

Thus, we only run getElementByID once - the first time a CSS style’s javascript expression is run. This worked - the DIV tag now scrolls much more quickly, certainly not so as users will notice any lag.

"malformed header from script"

So, in testing simplyXiangqi, I found I was getting error pages when I tried certain actions. The actions seemed to happen okay, but I got the standard Rail ‘Application Error’.

Looking in the logs, I was getting an error ‘malformed header from script’. ‘Curious’, I thought. The full error was of the form:
malformed header from script. Bad header=isRed = 1:
/home/simplyxi/public_html/dispatch.cgi

It seemed to me that part of my code was appearing as a header when the page was returned.

Eventually, I figured it out. My code still had ‘puts()’ calls in it. I’d added these for debugging during development on WebBrick. In WebBrick, the strings output by ‘puts()’ were written to the command window WebBrick was running under. On my Apache installation, though, this was output as a header. Consequently, I got errors.

I removed the ‘puts’ calls, and it all worked fine.

Comments from my old blog:

Thanks, buddy. Saved my sorry ass. :-)

By Magnus Bergmark at 18:07:07 Saturday 29th September 2007

Lock a column or ‘freeze panes’ in scrollable tables

This is quite clever - locking or freeze panes in HTML tables. This is something I’ve been asked to do a couple of times, and it’s hard. Fundamentally, web pages are not Excel.

Well, that solution works, although I guess it’s only useful in an intranet environment - it only works on IE. And it mystifies me why someone would want to or be allowed to run javascript from inside a CSS declaration. Still, we’ve checked it - works on IE 5.5 to 7.

Dispatch.cgi and End of File Characters

So, I was setting up my Chinese Chess site (domain name might not have propagated yet), and I had some problems. I kept getting the error ‘Application error: Rails application failed to start properly’. Looking in the logs the error was “Premature end of script headers: dispatch.cgi”. Tracked this down to the Dispatch.cgi file through Google, where other folks seem to have had problems with the EOF character.

I couldn’t figure out how to delete these characters - why is it that ‘vi’ is so popular? - so what I did in the end was create a new rails app and copy the cgi files across into my app. The files, although they appeared to have the same text, had very different file sizes.

Anyway, that seemed to do the trick.