Here is an example of a small report I threw together this evening. I
decided to mix shell script and J because sometimes those are the right
tools for the job.

I should note a discrepancy between
http://www.jsoftware.com/help/jforc/input_and_output.htm and the addons I
had installed: I had to use require 'format/printf' intead of require
'printf'. (I find it easier to locate something like printf from the
command line. Something like find /usr/share/j -iname printf.ijs just rolls
off my fingers - other people may have different sets of habits.)

Anyways, here's a slightly cleaned up version of the script (I removed some
directory names prefixing the file names and changed some file name
spellings to make them easier to read, and removed a bit of noise code
which probably would not interest this list.)

#!/bin/sh
# 0 :0
cd
fgrep -vf done.list calendar.list >todo.list
exec ijconsole "$0" "$@"
)

9!:29]1[9!:27'2!:55]1' NB. exit on error
require 'format/printf'

total=:_".(_4 {:: <;._2@,&'/');._2 fread 'calendar.list'
done=:_".(_3 {:: <;._2@,&'/');._2 fread 'done.list'
todo=:_".(_4 {:: <;._2@,&'/');._2 fread 'todo.list'

uy=:~.total

mat=: uy,.<:(#/.~>:uy,total),.(#/.~>:uy,todo),.(#/.~>:uy,done)
mat=: mat,.100*(3{"1 mat)%1{"1 mat

echo 'Year    Total   ToDo     Done   Pct Done'
     '%4d    %4d    %4d    %4d    %2.2g%%' printf mat

exit 0

In my opinion, printf is a bit easier to use than 8!:x for something like
this.

Note also that when I was writing this code I had two windows open - one to
test the script, the other to edit it in. Also, I commented out the exit 0
line and the 9!:29 line, and I had inserted set -vex in as the top line of
the shell script (after the #0 :0 line, of course).

In case anyone hasn't seen me presenting this idea before, what happens is
/bin/sh runs the script (that's the standard unix command line processor)
and it does a few things before turning control over to J. And then J runs
the same script.

What this gives me is a file that is small and easily describable (at least
in terms of purpose - you run this file and you get a report - much easier
to describe than "you run that file which is just a setup for this other
file").

The result of running this looks like:

Year    Total   ToDo     Done   Pct Done
2012    1464    1150     314    21.45%
2011    1163     950     213    18.31%
2010    1095     915     180    16.44%
2009    1096     841     255    23.27%
2008     703     411     292    41.54%
2013     730     288     442    60.55%
2007     186       1     185    99.46%

And, yes, I ran it a couple times to adjust the position of the column
headers over the numbers. That aspect was pure trial-and-error.

Anyways, I like that these two parts of the script are glued together.

Of course, another way to do this would be to run fgrep from inside J. I
don't think we have an emulation of fgrep -vf though, that performs very
well. But maybe I'm overlooking something?

Here's a general description of what I was looking for there:
given a list of boxed substrings X (e.g. 'ing';'cked';'lts') and another
list of boxed strings Y, return Y with all boxes removed where a box in X
contained a substring of box in Y. Also, for reference, X would have
contained 6437 boxes (and something like 423000 characters split across
those boxes) while Y would have contained 1885 boxes (and just under  29000
characters split across those boxes). The result would have had 4556 boxes
(and a total of just under 300k characters). Also in this particular case
each substring from X appeared exactly once in Y and was always in the
middle of a string in Y (non-empty prefix and suffix).

Anyways, fgrep -vf was a lot faster to type than even that description.

Thanks,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to