Dan Bron wrote:
One neat feature of J, if you're not familiar with it, is s: .
That primitive will allow you to encode strings as numbers (and decode them
back). That way, you can include "strings" in a homogenous numeric array,
and so avoid boxing & unboxing.
Excuse my newbie impatience, but can anyone point me toward the best
resource for learning s: ?? I looked at the Vocab. description but got
lost. I understood some of Dan's example:
For example, let's say we had some sales
records:
]T=:|: ('Paid' ; <"0] 100.00 71.30 451.60 12.32),~ _5 ]\ ;:
'Cust adam bob charlie dave Loc NY NY CA TX '
+-------+---+-----+
|Cust |Loc|Paid |
+-------+---+-----+
|adam |NY |100 |
+-------+---+-----+
|bob |NY |71.3 |
+-------+---+-----+
|charlie|CA |451.6|
+-------+---+-----+
|dave |TX |12.32|
+-------+---+-----+
Using s: , we could convert this to a homogenous numeric array, sans boxes:
s2i =: 6 s: s:
] M =: ( s2i@:(}:"1 <s2i@:(%7D:%221>) ,. >@:({:"1) ) }.T
1 2 100
3 2 71.3
4 5 451.6
6 7 12.32
if instead of a user wanting to query the numeric array on the currency
amounts, but instead wanted to query on one of the original string values,
say for total sales in NY, I realized I can use
s2i ' NY'
to return the corresponding code value (2) and use that to query. If I
submit a new string to s2i, say ' MO', it'll simply return the next
(previously unused) code value (8). Not apparent that this is a new code
being assigned vs. existing code being returned.
So now I can see how to query against my strings, not just numeric fields.
But what if I want to see all my coded strings, to find out if a string has
been coded already? Is there a way to view all of the code pairs?
Many thanks!
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm