Vocabulary/quotedot

From J Wiki
Jump to: navigation, search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

". y Do

Rank 1 -- operates on lists of y, producing a result of variable shape for each one -- WHY IS THIS IMPORTANT?



Executes the sentence y, returning the result of the sentence as the result of ". y. y is a list of characters.

The values in y must represent ASCII graphic characters unless they are in quoted strings.

   ". '6'
6
   ". '6 * 3'
18
   ". '< i. 3'
+-----+
|0 1 2|
+-----+

Common uses

1. Convert a number in literal form to a numeric type

but this is not the best way to do it - see below

   y=: '-1.386E_5'
   ". y
_1.386e_5

In practice  x ". y is better than  ".y for converting strings to numbers. But  ".y is usually safe enough for small strings

   ]array =: ".;._2 (0 : 0)
1 2 3   NB. each line is executed
3 1 4   NB. so comments here are ignored, just as in normal execution
5 2 3
)
1 2 3
3 1 4
5 2 3

2. Assign a non-noun value that you have in string form

The problem is, verbs can produce only noun results, so producing a verb value is problematic.

   verbdef =: '+&2'        NB. verbdef describes the verb we want: add 2
   ". 'vb =: ' , verbdef   NB. execute the sentence 'vb =: +&2'

   vb                      NB. It did it!  The verb is defined
+&2
   vb 5
7

3. Callbacks from a handler can be handled by passing a callback: sentence to the handler, which executes ".sentence at callback time.


Related Primitives

Numbers (x ". y)


More Information

1. An error encountered in the execution of y causes ".y to fail also.

2. If

  • the result of the sentence is not a noun, or
  • there is no executable sentence because y is empty or contains only whitespace or comment,

the result of ".y is 0$0, an empty Boolean list.

3. Converting characters to numerics is better handled with dyadic  x ". y .

4. The sentence y is executed in the same J context as that in which ".y appears, with access to the same namespaces.


Use These Combinations

Combinations using ". y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Convert an integer to a numeric list of digits Boolean, integer, extended integer "."0@": y @: & &: in place of @ fastest way, especially for extended integers


x ". y Numbers

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



Converts a byte array y into numbers.

Ill-formed numbers are replaced by x, which serves as a default value

   x=: __                    NB. minus-infinity
   x ". '2 -3 4e 5.6 _ .7'   NB. 4e is ill-formed. -3 and .7 are OK
2 _3 __ 5.6 _ 0.7

Common uses

1. Interpret an input stream of numbers given in character form (as above)

substituting 0 or some other value e,g, (_) for any ill-formed numbers

WARNING: Using Indeterminate (_.) as the default value x is not recommended, for reasons given on that page.

2. Define a number array inside a script in conveniently editable form

   ]array =: 0&".;._2 (0 : 0)
1 2 3
3 1 4
5 2 3
)
1 2 3
3 1 4
5 2 3

   ]array =: 0&".;._2 (0 : 0)
1 2 3  NB. better not try comments!
3 1 4  NB. everything is a number
5 2 3
)
1 2 3 0 0 0 0 0
3 1 4 0 0 0 0 0
5 2 3 0 0 0 0 0

More Information

1. x ". y does not execute y as a J sentence, but instead interprets it as describing numbers only.

2. Numbers in y are delimited by whitespace.

3. The rules for valid J numbers are relaxed as follows, to allow more conventional numerals:

  • a negative sign preceding a number, or the exponent of a number, can be either - or _
  • commas within numbers are ignored
  • fractions need not have a digit 0 before the decimal point
  • a number may be preceded by a + sign
  • the exponent of a number in exponential format may be preceded by a + sign.

Details

1. y may have any shape. Each 1-cell (that is, each row) of y is converted separately, but the results from the row are not collected in the usual way (this is why the right rank is infinite rather than 1). Instead:

with x replacing any invalid numerals. A row containing no non-whitespace is converted into an empty list of numbers

  • Each result list is extended to the length of the longest result list, by appending values of x
  • If the result lists are now 1 atom long, they are all converted to atoms
  • The results (atoms or lists) are collected into the final result using the frame of y with respect to 1-cells
   99 ". ''            NB. empty list, empty result

   99 ". '56'          NB. A single number is converted to an atom
56
   $ 99 ". '56'        NB. see? It's an atom

   99 ". '2 3',:'5.0'  NB. Second row is short and is extended
2  3
5 99
   ]a =. '2.0',:'5.0'  NB. A 2x1 table
2.0
5.0
   99 ". '2.0',:'5.0'  NB. special case: each row has 1 number, they are treated as atoms
2 5
   ]b =. ,. '123'      NB. Another way to do the same thing
1
2
3
   99 ". b
1 2 3
   99 ". '',:'5.0'     NB. first (empty) row is extended to length 1, then treated as atoms
99 5