Subject: Re: Please help with research
From: richard.levine@canrem.com (Richard Levine)
Date: Wed, 30 Aug 95 22:55:00 -0500
Organization: CRS Online  (Toronto, Ontario)

I am submitting the following hex dump program in J to
fulfill the requirements of the "research problem" posed
recently in the various computer language news groups.  The
program could be translated quite readily into all major APL
dialects.

The original program is the hexdump function supplied with
some J systems (in the format.js script). I have revised
with lots of comments for a larger audience.

Try the program.  It is robust.

I am submitting the program as is to the originator of the
problem.

However, I have a questions for internal discussion in
comp.lang.apl.

From the J and APL point of view, with its emphasis on array
processing, consider the overall method:

All the character data is translated into displayable
characters, all the hex representations of the original data
are computed, and all the line labels are computed.  Then a
while loop prepares the report one section at a time (each
section with "rps" lines), each section separated from the
following section by a blank line.

Question: Is it better in some APL sense to compute the
report entirely, and insert the blank separator lines for
the sections in a final step?  What are the "pros" and
"cons" of this?

Question: What are the "pros" and "cons" of computing the
report one line at a time?  (This might be more space-
efficient.)

I have often been puzzled by these considerations in APL
(and J), and would appreciate any insight into this matter
that can be provided.

--- thanks
R.

--- function output

   x =. hex_dump 128 take a.
   $ x
8 72
   x
00  00 01 02 03-04 05 06 07  08 09 0A 0B-0C 0D 0E 0F
|................|
10  10 11 12 13-14 15 16 17  18 19 1A 1B-1C 1D 1E 1F
|................|
20  20 21 22 23-24 25 26 27  28 29 2A 2B-2C 2D 2E 2F  |
!"#$%&'()*+,-./|
30  30 31 32 33-34 35 36 37  38 39 3A 3B-3C 3D 3E 3F
|0123456789:;<=>?|
40  40 41 42 43-44 45 46 47  48 49 4A 4B-4C 4D 4E 4F
|@ABCDEFGHIJKLMNO|
50  50 51 52 53-54 55 56 57  58 59 5A 5B-5C 5D 5E 5F
|PQRSTUVWXYZ[\]^_|
60  60 61 62 63-64 65 66 67  68 69 6A 6B-6C 6D 6E 6F
|`abcdefghijklmno|
70  70 71 72 73-74 75 76 77  78 79 7A 7B-7C 7D 7E 7F
|pqrstuvwxyz{|}~.|

--- function begin

hex_dump =. 3 : 0
NB. show character data in hex and displayable characters
NB. hexdump y where y is unboxed array of characters
NB. Version 1 / Chris Burke / 8 Feb 1995 / original
algorithm
NB. Version 2 / Richard Levine / 30 Aug 1995 / same method,
rewrite
NB. algorithm assumes there are 16 characters per report
line
NB. report is computed "rps" report lines at a time (may be
changed)
NB. quit now if empty argument and return empty report
if. 0 = */$ y. do.
   0 0$''
   return.
end.
NB. convert possible numeric argument to character (no error
report)
txt =. ": ,y.
r =. i. 0 0
bar =. 179{a.
hex =. '0123456789ABCDEF'
NB. Assume ascii 32 to 126 are displayable
NB. 127 {. a. gives characters ascii 0 to ascii 126
av =. (32#'.'), (32 }. 127 {. a. ), (256-127)#'.'
NB. sep are separators for hex representation
NB. sep is computed under assumption of 16 hex numbers per
line
sep =. ' ';'-';' ';'  ';' ';'-';' ';'  '
sep =. 3 1 3 1 3 1 3 1 # sep
NB. nrows is the number of rows in report (16 characters per
line)
nrows =. >. (#txt)%16
NB. lab is list of all row labels (label is line number in
hex)
NB. w is digits in largest row label (less 1)
w =. >. 16 ^. nrows
lab =. ((nrows,w)$,((w$16) #: i. nrows) { hex),.'0'
NB. dat is decimal representation of each character in text
dat =. a. i. txt
txt =. dat { av
NB. rows per section, characters (displayed) per section
rps =. 8
cps =. rps * 16
NB. rows is number of rows to be displayed in current
section
NB. chars is number of chars to be displayed in current
section
NB. both to be adjusted for final section
rows =. rps
chars =. cps
while. nrows > 0 do.
  NB. check if this is last section (may be incomplete
section)
  if. nrows <: rps  do.
    rows =. nrows
    chars =. #txt
  end.
  NB. r1 is row labels for this section (with trailing
blanks)
  r1 =. (rows {. lab),"1 (2#' ')
  NB. r2 is hex represenations for this section
  r2 =. (16 16 #: chars {. dat) { hex
  NB. pad r2 with blanks to fill out current number of rows
  r2 =. (rows*16) {. r2
  NB. format hex representations with separators
  NB. 50 under assumption of 16 and sep as computed above
  r2 =. (rows,50)$;(<"1 r2) ,&.> (rows*16)$sep
  NB. r3 are the characters to be displayed
  r3 =. bar ,. ((rows,16)$(rows*16) {. txt) ,. bar
  NB. catenate this section to report, append blank line
  r =. r,(r1 ,. r2 ,. r3),' '
  dat =. chars }. dat
  txt =. chars }. txt
  lab =. rows }. lab
  nrows =. nrows - rows
end.
NB. Drop last line (blank) and return report as result
}: r
)

--- function end

PS

the output should not have wrap-around lines,but the e-mail has short
width (output is 72 characters wide) also wathc out for warp-around
liens in the J text
--R.
