Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rich H.J for C programmers.2006.pdf
Скачиваний:
19
Добавлен:
23.08.2013
Размер:
1.79 Mб
Скачать

Monad ": also converts boxed operands to the character arrays, including boxing characters, that we have seen in the display of boxed nouns.

Format an Array: 8!:n

To format an entire array, perhaps for display using the Grid Control, use the 8!:n family of foreigns. x 8!:0 y formats each atom of y into a boxed character string under control of the format phrases in x :

'3.0,m<$(>p<$>n<)>q< >c14.2' 8!:0 (10 500.32 ,: 22 _123456)

+---

+--------------

+

| 10|

$500.32 |

+---

+--------------

+

|

22|

$(123,456.00)|

+

---+--------------

+

The format phrases are separated by commas; here there were two, the simple 3.0 and the more advanced m<$(>p<$>n<)>q< >c13.2 . The last thing in a format phrase is the field-width specifier w.d, which means what you expect. If w is 0, a computed width is used for the entire column, while if w. is omitted, the minimum width is used for each number individually.

Preceding the w.d field come the modifiers. They include the single letters c (put commas in the formatted number) and l (left justify), and a number of modifiers, called format strings, that have a character-string value. These are specified as m<string>, m(string), or m[string], where m is the one-character name of a format string— you pick a bracket character <, (, or [ that doesn't occur in your string.

The format-string modifiers replace the default format strings for the column, except for the s modifier described presently. The format strings have the names b, d, m, n, p, q, r, and s. The default values are s='e,.-*', b='0', d=infinity, m=(2{s if n is omitted, '' if n is given), others='' . The s modifier is set differently: it is in the form s[abab...] where each a character is one of the default e,.-* and the following b character replaces that a character in the format string.

A number is classified according to its value; the value is converted to the value string v according to w.d; then the formatted result is created from a combination of the format strings and the value string, based on the value:

Infinite/indeterminate: d (default is _, __, or _. according to value) Negative: mvn

Zero: b Positive: pvq

The elements of s give characters to use for exponent, thousands separator, decimal point, minus sign, and insufficient field-width. If the formatted result calculated above is shorter than the field width, the field is first initialized with the r format string, repeated cyclically as necessary, and the formatted value is overlaid on that.

129

There are three verbs in the 8!:n family. All have infinite rank. 8!:0 produces a box for each atom of y, 8!:1 produces a box for each column of y, and 8!:2 produces an unboxed character array:

+

'3.0,14.2' 8!:0 (10 500.32 ,: 22 _123456)

---+--------------

+

| 10|

500.32|

+

---+--------------

+

| 22|

-123456.00|

+

---+--------------

+

+

'3.0,14.2' 8!:1 (10 500.32 ,: 22 _123456)

---+--------------

+

| 10|

500.32|

| 22|

-123456.00|

+

---+--------------

+

 

'3.0,14.2' 8!:2 (10 500.32 ,: 22 _123456)

 

10

500.32

 

22

-123456.00

Format binary data: 3!:n

If you need to write numbers to files in binary format, you need something that will coerce your numbers into character strings without changing the binary representation. J provides the foreigns 3!:4 and 3!:5 for this purpose. Each is a family of conversions, invoked as a dyad where the left operand selects the conversion to be performed.

2 (3!:4) y converts the numeric list y to a character string, representing each item of y by 4 characters whose values come from the binary values of the low-order 32 bits of y :

16b31424334

826426164

This is an integer whose value is 0x31424344. We can convert it to a character string:

2 (3!:4) 826426164 4CB1

The 4 characters (with values 0x34, 0x43, 0x42, 0x31) correspond to the bits of the number 826426164 in little-endian order.

We can use a. i. to look at the codes for each character in case they are not printable:

a. i. 2 (3!:4) 1000 100000 232 3 0 0 160 134 1 0

232 3 0 0 corresponds to 1000 and 160 134 1 0 to 100000 .

_2 (3!:4) y is the inverse of 2 (3!:4) y, converting a character string to integers:

_2 (3!:4) '4CB15CB1' 826426164 826426165

The other integer conversions are similar. bits of each item of y to 2 characters, and _1

1 (3!:4) y converts the low-order 16 (3!:4) y converts back to integers, 2

130

characters per integer. 0 (3!:4) y is like _1 (3!:4) y but the integers are unsigned (i. e. in the range 0-65535).

The floating-point conversions are analogous. 2 (3!:5) y converts each item of y to 8 characters representing long floating-point form, and _2 (3!:5) y converts back; 1 (3!:5) y and _1 (3!:5) y use 4-character short floating-point form.

printf, sprintf, and qprintf

When you need formatted lines for printing you may feel at home using printf and sprintf, which work like their C counterparts. printf displays a line, while sprintf produces a string result:

'The population of %s is %d\n' printf 'Raleigh';240000 The population of Raleigh is 240000

s =. 'The total of %j is %d.\n' sprintf 1 2 3;+/1 2 3 s

The total of 1 2 3 is 6.

You need to execute load 'printf'

to get the printf verbs defined. J's printf contains a few features beyond C's such as the %j field type seen above. You should run the Lab 'Formatting with printf' for details.

One feature with no analogue in C is qprintf, which produces handy typeout for debugging:

a =. 3 4 5 [ b =. 'abc';'def';5 [ c =: i. 3 3 qprintf 'a b c '

a=3 4 5 b= +---+---+-+ |abc|def|5| +---+---+-+ c=

0 1 2

3 4 5

6 7 8

qprintf is described in the 'Formatting with printf' lab.

Convert Character To Numeric: Dyad ".

Dyad ". has infinite rank. x ". y looks for numbers in the words of y, which must be a character array. Words representing valid numbers are converted to numbers; words not representing valid numbers are replaced by x . If 1-cells of y have differing numbers of numbers, x is used to pad short rows. 'Valid numbers' to dyad ". are anything you could type into J as a number, and more: the negative sign may be '-' rather than '_'; numbers may contain commas, which are ignored; and a number may start with a decimal point rather than requiring a digit before the decimal point. With the

131

relaxed rules you can import numbers from a spreadsheet into J, using x&". to convert them to numbers:

999 ". '12 .5 3.6 -4 1,000 x25'

12 0.5 3.6 _4 1000 999

All legal numbers except for 'x25' .

If your character string is a valid J number, either monad ". or dyad ". can be used to convert it, but dyad ". is much faster.

132