I am interested in porting some of my APL workspaces from
APLX and Sharp APL
for Linux to J. I expected it to be fairly easy to learn
J due to the
similarity between the two languages, but it has turned
out to be harder than
I thought, partly due to the different terminology, e.g.
verbs, conjugations,
adverbs, etc. as opposed to functions and operators,
different names for
equivalent functions, and so on. One area I am looking at
now is how to
format output.
The format primitive ": seems similar to APL thorn, except
that it appears not
to support any equivalent to APL2 picture format.
Looking at the documentation for the format facility 8!:n,
it seems to be a
limited subset of APL #FMT. In particular, it seems to
lack the Z (zero fill)
modifier and the G and <...> (insert arbitrary text)
format specifiers. This
makes it more difficult to intermingle text and numbers,
an operation that one
commonly needs to perform when formatting output.
For example, suppose that I want to display a date or time
in an easily
readable format. In APL I could enter something like
'06:06:06' {thorn} #TS[4 5 6]
or
'ZI2,<:>,ZI2,<:>,ZI2' #FMT #TS[4 5 6]
In J, the best I have been able to come up with is
}.,'r<:00>3.0' (8!:2) <. _3{. 6!:0 ''
but this is something of a kluge, depending on the fact
that the components of
the time are always nonnegative. If I tried to use a
similar technique to
format other base 60 values that could be negative, such
as angles in
degrees, minutes, and seconds of arc, I would get into
trouble, since the
background fill appears to the left of the sign, so I
might end up with
something like 00-3:07:32. Here, having a Z modifier
available would clearly
be better that faking it with background fill.
Or suppose that I had some telephone numbers expressed as
integers, and I
wanted to format them like (702) 888-1375. In APL I could
say
'G<(999) 999-9999>' #FMT PHONENUMS
I see no equivalent functionality provided by 8!:n. The
8!:n formatter would
be much more useful if the Z, G, and <...> text insertion
from #FMT were added
to it.
The best option I see right now is to use printf, which I
was told about when
I posted about J formatting in comp.lang.apl. I hadn't
known about this,
having not seen it in the documentation. So I might type
'%2.2d:%2.2d:%2.2d' printf <. _3{. 6!:0 ''
(much like in C) to display the time of day. The printf
function doesn't have
any equivalent to the G format specifier, but it does
provide the equivalent
of <...>. It should provide the equivalent of Z as well,
but this doesn't
work for negative numbers:
'%3.3d' printf _4
results in
0-4
which is clearly not the desired result.
The equivalent C program:
#include <stdio.h>
int main () {
printf ("%3.3d\n", -4);
}
compiled with the Gnu compiler collection, prints
-004
which is a more reasonable result.
By the way, I rarely find the J documentation index to be
of any use since,
like the indices to most software documentation, it is
woefully incomplete.
If you look under F, there is no entry at all for
formatting. If there was, I
would expect it to point to information on all of the
formatting facilities
provided by J, including ":, 8!:n, and printf. It would
also be useful to
have entries for APL terms such as find, scan, and so on,
with pointers to
documentation on the J equivalents.
Including more examples in the documentation would also be
useful. For
example, I have been shown that monadic 8!:n can be
applied to a boxed array
of numbers and strings, but I have seen no examples
showing how to use dyadic
8!:n for this. What format specifier is used for the
columns containing text?
Suppose I want to display a list of location names
followed by the latitudes
and longitudes of those locations (e.g. 'Cranford,
NJ';40.659;-74.305). What
would be an appropriate left argument for 8!:n?
--- Brian
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm