Monday, March 05, 2007

Problem

You want to break up a string into its individual tokens or words

Solution

I’ll present the solution in two forms; the first will use “traditional” Object Pascal idiom, the second will make the most of Chrome’s features where appropriate. Both solutions will use the FCL’s string.Split() method.

Object Pascal Version

namespace Cookbook1_1a;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
var
  userRec    : String;
  attributes : array of String;
begin
  userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  attributes := userRec.Split( Space );
 
  for each token : String in attributes do
      Console.WriteLine( token );
end;

end.

Chrome Version

namespace Cookbook1_1b;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
begin
  var userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  var attributes := userRec.Split( Space );
 
  for each token in attributes do
      Console.WriteLine( token );
end;

end.

Notice how the Chrome version doesn’t have any type declarations, Chrome’s type inference is doing all the work of figuring out what the types are at compile time. One other point to note is that Chrome allows the developer to declare their variables in-line, where they are needed and does not require all local variable to be declared at the top of the method, procedure or function as classic object pascal compilers do.

Finally, the loop at the bottom is there so that I can see that the string has been split as I’d expect. A unit test would be better, however.

posted on Monday, March 05, 2007 8:50:25 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback

The sample was compiled using the latest version of D, 1.007 at the time of writing. You can get the latest version of the D compiler here.

Problem

You want to break up a string into its individual tokens or words 

Solution

Use the split() function from std.string, like so:

import std.stdio;
import std.string;

void main(char[][] args)  {
     char[]   record     = "01 Barry Carr 17/04/1964 barry@notarealdomain.com";
     char[][] attributes = split(record);

     foreach( char[] attribute; attributes )
        writefln( attribute );
 }

There is on overloaded version of split() that allows you specify what characters are to be used as delimiters.

The foreach statement at the end of the code above is there so that the tokens can be displayed on the console.

Tags: ,
posted on Monday, March 05, 2007 7:02:39 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Sunday, March 04, 2007

Taking a leaf out of Gary Short’s (cook)book and blatantly stealing his idea, I’m going to follow his example and start some programming cookbooks of my own. Like Gary, my cookbooks will be based upon the items featured in the Python Cookbook. Gary is currently doing a cookbook for Smalltalk, one of my favourite programming languages, so I’ll do cookbooks for D, Chrome and possibly F# and OCaml. I’m interested in learning all of these languages so undertaking these cookbooks should help me to gain a wider understanding of them and also enable me to share my experiences with a wider audience.

The Languages

D is the creation of Walter Bright, the man that developed the worlds first C++ compiler (before Walter, all C++ “compilers” were C preprocessors). D’s aim is to overcome some of the short-coming of C++. D offers virtually all the features you’d expect from a modern language, including: Templates (generics), Delegates, Interfaces, Mixins, (optional) Garbage Collection, Type Inference, Design by Contract, Tail Recursion and much, much more. The D compiler is free to down load and use and is available for Win32 and Linux.

Chrome is an Object Pascal implementation targeted at the .NET and Mono platforms. It’s being developed by RemObjects. Chrome offers all the features of C# 2.0 as well as Design by Contract, Type Inference and Virtual Constructors. It also improves on “classic” Object Pascal implementations by improving the with statement and by adding the ability to declare variables in-line, like the C family of languages. Chrome is a commercial product but RemObjects do provide a free, command line compiler.

OCaml is an Object-oriented functional/imperative language hybrid developed and maintained by INRIA, France's national research institute for computer science. OCaml can be interpreted or compiled very efficiently into native code. Like all the languages mentioned so far, OCaml supports Type Inference, Garbage Collection and Generics. Also, as you would expect from a functional language, OCaml supports: Tail Recursion, Pattern-matching and Currying.

F# is an experimental functional language developed by Microsoft and, as you’d expect, F# is targeted at the .NET platform. F# is based on OCaml.

Tags: , , ,
posted on Sunday, March 04, 2007 8:06:44 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback

Procupine Tree - In Absentia, album cover"In Absentia" is Porcupine Tree's (PT) 12th album, it was released in Sept 2002. I only discovered PT about 12 months ago and I’ve snapping up all of their albums ever since.

PT are a British Progressive Rock band that formed in the ‘90s and it is the brain child of Steve Wilson. It’s said that PT are one of the principle bands that kept Prog-Rock alive in the UK.

I know Prog-Rock is a dirty word to a lot of people, but I can assure you that there are no 20 minute drum solos on this album, just excellent tracks with good lyrics played by talented musicians.  A lot of the tracks are quite dark, not as dark as, for instance, Nine Inch Nails, but certainly not the usual “boy meets girl” type of stuff. If you’re not into darker themes then PT probably isn’t for you, however, you’ll never know unless you give them a spin. I’d recommend this ablum or their latest album, Deadwing. There are some MP3 samples of Deadwing at PT’s website.

You can find out more about the band at http://www.porcupinetree.com. Or, you can have a look at their Wikipedia entry here: http://en.wikipedia.org/wiki/Porcupine_Tree. For the album lyrics of “In Absentia” go here: http://www.porcupinetree.com/discography.details.cfm?albumid=12.

Enjoy.

posted on Sunday, March 04, 2007 7:14:16 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Saturday, March 03, 2007

It seems that the WordPress team have been having problems, again: http://wordpress.org/development/2007/03/upgrade-212/

posted on Saturday, March 03, 2007 12:19:01 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Thursday, February 22, 2007

I can't believe that the people that brought us Delphi, JBuilder and C++Builder have gone and done this: http://www.codegear.com/Products/Delphi/DelphiforPHP/tabid/237/Default.aspx

A PHP IDE!

It's no wonder that Delphi and the BDS is lagging behind the likes of Chrome and Visual Studio when they are frittering their time away on nonsense like this. Why didn't they expend their effort on something up-and-coming like Rails and not on a spent force like PHP?  A decent Rails IDE would have flown off the shelves.

It seems that my evil plan to eradicate PHP has taken a step back - curses

posted on Thursday, February 22, 2007 4:25:04 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Tuesday, February 20, 2007

You may have noticed that this blog has changed. For a start, it looks different and there are no postings on it. Thats because I've has some problems with WordPress, my original blogging system, and was forced to get rid of it. A pain, but there you go.

I used this imposed change as an excuse to find a non-PHP based system. As a developer, I really don't like PHP, I think its a fetid heap of parrot droppings - not so much as a considered reaction, more of a viseral dislike. 

Ideally, I would have liked to have found a Rails based solution, but sadly, my host doesn't yet support Rails hosting. So, being as I develop it .NET most of the time and as ASP.NET is supported by my provider, I thought I'd hunt out an ASP.NET based system blogging instead. And here it is, dasBlog, an open-source ASP.NET blogging system.

Admittedly, its not quite as easy to set up as WordPress, you may need to get your hosting provider to set some permission for you, and you may also need to make some changes to your sites web.config file, but once thats been done then you're good to go. So far, dasBlog seems to be OK, time will tell.

As there now seems to be a whole new generation of more grown-up web development systems appearing on the scene we should hopefully start to see a sharp decline in the use of PHP. And not a moment too soon in my view.  Bias aside, PHP is an insecure, sloppy and inconsistent language that just encourages laziness (OK, that was biased). Hopefully, the newer, more rational, better designed and enlightened frameworks like: Ruby on Rails; Django; TurboGears; Grails and even ASP.NET will give PHP the kicking it so well and truly deserves and punt into computing obscurity.

I hope I have contributed in a very small way to the demise of PHP by drawing my own, faint, line in the sand. Remember people: PHP, together we can fight it.

posted on Tuesday, February 20, 2007 3:03:20 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Wednesday, July 20, 2005

Be sure to visit all the options under "Configuration" in the Admin Menu Bar above. There are 16 themes to choose from, and you can also create your own.

 

posted on Wednesday, July 20, 2005 7:00:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback