JwithStrings/Introduction
J with Strings: Introduction
Back to: Table of Contents
This is a cookbook for handling and processing strings in J. Or, as J-ers call it: literal data.
There is plenty of instructional material about handling arrays of numbers in J. Arrays of characters are handled just the same. End of story, right?
Wrong.
To be sure, J primitives do act on alphabetic letters in the same way as they do on numbers. That is, where it is sensible for them to do so. Thus you wouldn't expect Plus to do anything sensible with 'a'+1, unless the language were implicitly to treat an ASCII character as the numeric value of its binary code (as C/C++ actually does).
But how do I take my knowledge of matrix theory, as expounded in Spiegel say, and use it to convert a text file marked up in BBCode or MoinMoin to HTML? The fact is, I can't, to any useful extent. Some specialised knowledge is required, appropriate to BBCode, MoinMoin or HTML.
Hence the reason for this cookbook.
Literal data comes with its own special sets of problems. These problems were severe enough when literal data was ASCII-only. But nowadays there's Unicode to consider. And there are several ways of mixing Unicode with ASCII characters, the best-known being utf-8. This is the one which j602 supports, in company with most internet browsers and up-to-date word processors.
This book focusses on literal data rather than numerical data. We will cover not only ASCII strings (byte codes 0 to 127) but Unicode too, both utf-8 and so-called wide characters.
If you're writing an app (application) for distribution, even if it's a mathematics-intensive app such as a statistics package, you are going to need to communicate with your end-user in words, even if only for error messages and button tops. The code to communicate with the end-user can be as much as 50% to 99% of the total code you write, even for a heavily mathematical app. The time and effort to design, code and debug it may represent an even greater proportion.
The J literature however does not reflect this, being heavily biassed towards numbers. This reflects the concerns of its traditional users: statisticians and number-crunchers.
Do not think, however, that J is second-rate at handling literal data. It is among the best of languages for this purpose. The J literature simply does not emphasise this fact.
The designers of J have put a lot of thought into representing mathematical algorithms concisely. There are primitives to generate primes, Taylor's series and permutations, and to perform matrix inversion. In consequence the number handling code in your app will pack a lot of well-understood functionality into a very few lines.
The same cannot be said for the code that delivers this functionality to the user in words. It will be terser than equivalent code in C or Visual Basic, but not conspicuously so. It will rely more on add-ons like regex than on the intensive use of cunning primitives.
A teacher wanting to write a simple app to use in class won't be too worried by this. He or she will usually be content to be able to do in J the sort of string-handling they can already do in Visual Basic, or maybe C/C++.
Indeed the teacher will wish to concentrate on the mathematics and its exposition, and will have little appetite to wrestle with code whose purpose is to act as a vehicle for the teacher's words.
This book tries to meet that need by de-skilling the task of delivering verbal material, as opposed to writing it in the first place.
However we will also demonstrate the grace that J brings to number-handling in the service of frequently encountered examples of literal data:
- natural language
- html and xml
- date and time
- currency
- electronic components
- bioengineering
- scientific notation; hexadecimal
- angles (degrees / minutes / seconds)
- plus the string-processing of (J) source code itself.
There are a number of good standard textbooks on J. Some are distributed free with J, and you see links to them when you invoke J Help:
>> << Ndx Usr Pri JfC LJ Phr Dic Rel Voc !: wd Help
We shall avoid replicating their material, instead providing references to their relevant sections where appropriate.
The numerical needs of the J beginner are well-served by the standard texts. This book complements them in the realm of literal data.
-- Ian Clark <<DateTime(2012-08-28T13:13:42Z)>>