Thursday, March 3, 2016

Nostalgic Programming in Pascal

A writer once said that in the new world, programmers would be free to choose any programming languages to do their job with. They would be able to use the most productive language for themselves and also for the task at hand. In this event, I am feeling a bit nostalgic, and found that there is an open source software called Free Pascal Compiler. In the past I learnt programming using Pascal as my second language, specifically Turbo Pascal.

Background

Needing to write a simple program to verify that a CSV file have the specified number of columns. It could be done using awk etc, but I need the program to be fast because the file size is large and the rows is in the order of hundreds of thousand. 

The program


Explanation

At first the field counter returns strange result that very much different than the expected one. I was baffled, until I remembered that pascal Strings have maximum 255 characters wide (the files have more characters in each line). So it turns out to be a switch ($H+) to enable null terminated AnsiString in FPC instead of normal 255 character strings (aka ShortString). See http://wiki.freepascal.org/Character_and_string_types :

The type String may refer to ShortString or AnsiString, depending from the {$H} switch. If the switch is off ({$H-}) then any string declaration will define a ShortString. It size will be 255 chars, if not otherwise specified. If it is on ({$H+}) string without length specifier will define an AnsiString, otherwise a ShortString with specified length. In mode delphiunicode' String is UnicodeString.

Compiling 

In my ubuntu system, I installed fpc using apt-get.
 apt-get install fp-compiler-2.6.2

Then all you do is fpc filename.pas :

The warning 'contains output sections' is fixed in later version of fpc, but for this version it is harmless.

Conclusion

You don't need to seek Turbo Pascal anymore to do Pascal programming, now you can use free pascal.

No comments: