I was working on restoreXLS2JDB and I realized that I used a verb not in any of
the JAL or System:
mat2boxedlist=: 3 : 0
dat=. <"1 |: ,each y
opn=. > each dat
emp=. isempty &> opn
num=. bx emp < 2 ~: 3!:0 &> opn
dat=. (, each num { opn) num } dat
ndx=. (i.#dat) -. num
(blankout each ndx { dat) ndx } dat
)
blankout=: 3 : 'boxEMPTY (bx y = <,'' '') } y'
boxitems=: <"_1 ^: (0: = L.)
boxedlist2mat=: [: |: boxitems &>
For example, you have a data that looks like this:
[data=. |:><every each 1 2 3 4 5;'abcde';6 7 8 9 10
+-+-+--+
|1|a|6 |
+-+-+--+
|2|b|7 |
+-+-+--+
|3|c|8 |
+-+-+--+
|4|d|9 |
+-+-+--+
|5|e|10|
+-+-+--+
mat2boxed will convert this to a list while respecting the datatype like so:
mat2boxedlist data
+---------+-----------+----------+
|1 2 3 4 5|+-+-+-+-+-+|6 7 8 9 10|
| ||a|b|c|d|e|| |
| |+-+-+-+-+-+| |
+---------+-----------+----------+
Running boxedlist2mat will do the reverse:
boxedlist2mat mat2boxedlist data
+-+-+--+
|1|a|6 |
+-+-+--+
|2|b|7 |
+-+-+--+
|3|c|8 |
+-+-+--+
|4|d|9 |
+-+-+--+
|5|e|10|
+-+-+--+
My question now is if we already have verbs that does the exact same thing in
JAL or the SYSTEM libraries? I've been searching and not getting anywhere.
Thanks.
r/Alex
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Alex Rufon
Sent: Wednesday, December 02, 2009 12:17 AM
To: Programming forum
Subject: Re: [Jprogramming] JDB Utilities: Export to Excel
Just got home ...
Hehehehe ... yes, I use 'primitives' a lot. Let me emphasize that ... A LOT. :P
I find it way much easier to maintain J code this way rather than the actual
symbols.
I'll give inverted tables a try with tara ... I sort of used the current excel
formatting so that I can "edit" data using excel interface. Trying to kill 2
birds with 1 stone.
Well, regarding the cpu hungry operation ... what I was trying to do was
transfrom JDB data from this format:
[data=. 1 2 3 4 5; (;:'aa bb cc dd ee'); 6 7 8 9 10
+---------+----------------+----------+
|1 2 3 4 5|+--+--+--+--+--+|6 7 8 9 10|
| ||aa|bb|cc|dd|ee|| |
| |+--+--+--+--+--+| |
+---------+----------------+----------+
to this format:
[data=. |:>(< every 1 2 3 4 5); (;:'aa bb cc dd ee'); << every 6 7 8 9 10
+-+--+--+
|1|aa|6 |
+-+--+--+
|2|bb|7 |
+-+--+--+
|3|cc|8 |
+-+--+--+
|4|dd|9 |
+-+--+--+
|5|ee|10|
+-+--+--+
My initial solution, I just boxed each of the numeric items like so:
NB. Find out first which columns are not boxed
[data=. 1 2 3 4 5; (;:'aa bb cc dd ee'); 6 7 8 9 10
+---------+----------------+----------+
|1 2 3 4 5|+--+--+--+--+--+|6 7 8 9 10|
| ||aa|bb|cc|dd|ee|| |
| |+--+--+--+--+--+| |
+---------+----------------+----------+
[mask=. I. -. (<'boxed') = datatype each data
0 2
[temp=. < every each mask { data
+-----------+------------+
|+-+-+-+-+-+|+-+-+-+-+--+|
||1|2|3|4|5|||6|7|8|9|10||
|+-+-+-+-+-+|+-+-+-+-+--+|
+-----------+------------+
NB. Save formatted data back
[data=. |:>(temp) mask } data
+-+--+--+
|1|aa|6 |
+-+--+--+
|2|bb|7 |
+-+--+--+
|3|cc|8 |
+-+--+--+
|4|dd|9 |
+-+--+--+
|5|ee|10|
+-+--+--+
NB. Check the datatypes
datatype each data
+-------+-------+-------+
|integer|literal|integer|
+-------+-------+-------+
|integer|literal|integer|
+-------+-------+-------+
|integer|literal|integer|
+-------+-------+-------+
|integer|literal|integer|
+-------+-------+-------+
|integer|literal|integer|
+-------+-------+-------+
As you can see, the numeric datatypes were maintained ... unfortunately this
code is soooo slooooowwww when you have a lot of rows of data
temp=. < every each mask { data
To speed it up, I converted the data to strings first before I boxed them like
so:
[temp=. cutopen each ": each mask { data
+-----------+------------+
|+-+-+-+-+-+|+-+-+-+-+--+|
||1|2|3|4|5|||6|7|8|9|10||
|+-+-+-+-+-+|+-+-+-+-+--+|
+-----------+------------+
Unfortunately, I lost the non numeric datatype in the end as a result
datatype each |:>(temp) mask } data
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
Is there another way of doing this? Thanks for any help. :)
r/Alex
P.S.
Just curious, why is converting the numeric array to string faster than keeping
them as numbers and boxing them?
________________________________________
From: [email protected] [[email protected]] On
Behalf Of bill lam [[email protected]]
Sent: Tuesday, December 01, 2009 10:53 PM
To: [email protected]
Subject: Re: [Jprogramming] JDB Utilities: Export to Excel
I became J-illiterate when reading your real-file code. ;-)
Nevertheless tara support inverted table and jdb is inverted table.
and there can have no reason for inefficiency. (untested)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm