Hello all -

I've recently started learning J, and need to get some practice.  I'm learning 
at home, and I have no colleagues to bounce ideas off.  Therefore, please tell 
me what you make of my efforts.  I run on Win XP.  I've installed MySQL at 
home, and have much experience of using SQL on Oracle 7 databases.  However, J 
is my first non von-Neuman language.  I've lots of experience of 'C' and PL/M.  

For an exercise I thought I'd attempt to reproduce finding sums and averages of 
numeric columns.  I started with the J studio for simple ODBC, and successfully 
loaded the MySQL ODBC handler.  From the studio I got all the dd lines, and 
could retreive rows from the table and display them in boxed format.  Here is 
what I had...

   load 'dd'
   (<'MySQL') e. {."1 ddsrc ''
   ch =: ddcon 'dsn=MySQL;modifysql=0;uid=scott;pwd=tiger'
   sh =: 'select purch_ref, from_whom, description, category, ir_category, 
date_format(date_purch,''%Y-%m-%d''), amount, pay_method from purchases2' ddsel 
ch
   dat =. ddfet sh,5
   dat                  NB. Display the first 5 rows, fetched by ddfet
   ddend sh
   dddis ch

The table is a table of business purchases.  For interest, MySQL dates need 
formating, as simple date columns cause an error: ISI09 Unsupported data type.  
The column called "amount" contain the money paid, in GBP.  I wanted to 
manipulate that column, and by a process of trial and error (much error!) ended 
up with this...


   ". > {. 6 }. |: dat          NB. Display the contents of column 6 (which 
contains numbers) as numbers
   +/ ". > {. 6 }. |: dat       NB. Sum of column 6
   (+/ % #) ". > {. 6 }. |: dat NB. Average of column 6

NB.  To unpack the sentence (+/ % #) ". > {. 6 }. |: dat 
NB.  we explain as follows:
NB.    dat    The boxed result of the select statement
NB.      |:   Transpose it so that the 1st column now lies along the 1st row
NB.     6 {.  Cut off the top 6 rows so that the 1st row is now the amounts
NB.      {.   Cut off the bottom row which contains nulls - and is so in the 
original database table
NB.      >    Unpack to a column of strings, which look like numbers
NB.      ".   Convert string to numbers

It seems that MySQL and J504 on Windows XP deliver strings of characters that 
"look like" numbers.

So, is my sentence   ".>{.6}.|: dat  good J, or can it be improved?  

I really struggled for a while to develop that sentence.  I see from another 
post in the J programming forum that someone else would like precedence, 
pretty-printing or other help from the J interpreter.  What I find difficult is 
the dimensions of the lists/tables and the rank of the operators.  Also, the 
names of the verbs (eg }. for cutting of the head of a list) makes learning 
awkward.  Any suggestions for these neophyte problems?

Thanks for your replies.

Richard Cunningham, Reading, England.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to