I know I've been on this path before but how did you guys do the conversion
from string to numbers (and vice versa) in APL? Can we replicated this in J?
Right now, I'm doing this by making a global vector of unique strings I've
encountered.
DICTIONARY_z_ =: < every a.
search=: verb define
data=. , boxopen y.
DICTIONARY_z_=: ~. DICTIONARY_z_,data
DICTIONARY_z_ i. boxopen y.
)
NB. boxopen is defined as:
NB. boxopen=: <^:(L. = 0:)
NB. Sample string data
[data=. (;: 'aa c3 d5 ae af ax ac ee'),<'The answer is 42'
+--+--+--+--+--+--+--+--+----------------+
|aa|c3|d5|ae|af|ax|ac|ee|The answer is 42|
+--+--+--+--+--+--+--+--+----------------+
NB. Test data is 2 dimensions
[testdata=. 8 3 $ data
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
|ac|ee|The answer is 42|
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
|ac|ee|The answer is 42|
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
NB. Numeric equivalent of the strings
[asnumbers=. search testdata
265 257 258
266 267 268
269 263 264
265 257 258
266 267 268
269 263 264
265 257 258
266 267 268
NB. Check if the numbers convert to strings
asnumbers { DICTIONARY
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
|ac|ee|The answer is 42|
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
|ac|ee|The answer is 42|
+--+--+----------------+
|aa|c3|d5 |
+--+--+----------------+
|ae|af|ax |
+--+--+----------------+
NB. Switch the columns
DICTIONARY {~ 1 2 1 { "1 asnumbers
+--+----------------+--+
|c3|d5 |c3|
+--+----------------+--+
|af|ax |af|
+--+----------------+--+
|ee|The answer is 42|ee|
+--+----------------+--+
|c3|d5 |c3|
+--+----------------+--+
|af|ax |af|
+--+----------------+--+
|ee|The answer is 42|ee|
+--+----------------+--+
|c3|d5 |c3|
+--+----------------+--+
|af|ax |af|
+--+----------------+--+
NB. Only get rows that's equal to 'aa' in the first column
[akey=. search 'aa'
265
[arows=. akey = {. "1 asnumbers
1 0 0 1 0 0 1 0
DICTIONARY {~ arows # asnumbers
+--+--+--+
|aa|c3|d5|
+--+--+--+
|aa|c3|d5|
+--+--+--+
|aa|c3|d5|
+--+--+--+
NB. Or simply
DICTIONARY {~ asnumbers #~ (search 'aa') = {. "1 asnumbers
+--+--+--+
|aa|c3|d5|
+--+--+--+
|aa|c3|d5|
+--+--+--+
|aa|c3|d5|
+--+--+--+
As you can see, it works. But I'm curious if there is another (better?) was to
do it. The only requirement is that the numbers generated can be processed to
get the original strings.
Thanks.
r/Alex
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Björn Helgason
Sent: Wednesday, February 17, 2010 5:59 PM
To: Chat forum
Subject: Re: [Jchat] Multiple cores
2010/2/17 Alex Rufon <[email protected]>:
> But I'm still transferring and loading a lot of data on the sub processes and
> this is when I got this idea of converting strings to numbers.
It was/is a common practice in APL to convert strings to numbers and
work with the numbers (integers) until you needed to present the
results in human readable form.
There are huge databases built every night at many companies building
such inverted databases to work on with APL systems.
The APL inverted processes built this way are very fast and worth the
slow processes doing the inversion.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm