Getting the type of a value in APL

Viewed 246

I have a long vector which should be a character vector but when I print it using Dyalog's DISPLAY function it turns out to be a mixed vector. Now I need to find out which of the elements is not a character. How do I retrieve the type of a value in APL?

3 Answers

Use ⎕DR (Data Representation) to check the type of things. For a char-vec, it's 82 (on a 32-bit interpreter) or 80 (64-bit) - and since the 64bit-platform supports unicode, it could be 160 or 320 as well. A nested vector is 326.

NB: you can also use ⎕DR¨ to investigate which element is not as you expected...

I'm on APL2. Naïvely I'd go for

X≡¨⍕¨X

Numeric values get a 1 and character values get a 0.

Like this

The expression* ⊃0⍴⊂A gives you the type of A. The type of an array is, simply put, a copy of the array but all content recursively replaced with zeros for numbers and spaces for characters. This means that you can compare the type with 0 to find numbers.

Take for example the following vector with a somewhat deceptive default display form:Try it!

      ⊢v←'abc',1 2,'de 3 4',5
abc 1 2 de 3 4 5

The boxed display form doesn't tell you the type of each element of a simple vector; it only indicates that the array has mixed type with a + in the bottom left corner:Try it!

      ]display v
┌→───────────────┐
│abc 1 2 de 3 4 5│
└+───────────────┘

Now we find the type:Try it!

      ⊢t←⊃0⍴⊂v
    0 0        0

We can stack it on top of the original vector to point at the numbers:Try it!

      ↑t v
    0 0        0
abc 1 2 de 3 4 5

Or we can compare it to zero to get a mask for the numbers:Try it!

      ⊢m←0=t
0 0 0 1 1 0 0 0 0 0 0 1

Finally, we can get the indices where** there are numbers:Try it!

      ⍸m
4 5 12

* If you are using ⎕ML←0, then ⊃0⍴⊂ can be written as the single primitive function
** If you are using version 15.0 or earlier you will have to write {(,⍵)/,⍳⍴⍵} instead of

Related