2

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?

Adám
  • 6,573
  • 20
  • 37
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60

3 Answers3

3

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...

MBaas
  • 7,248
  • 6
  • 44
  • 61
  • That did the trick, thanks! Strangely enough, for the vector *v* in question, ⎕DR¨ *v* returns a vector where all elements are 82, despite *v* being a mixed vector rather than a character vector. – August Karlstrom Jan 15 '19 at 10:00
  • It may well be that one of the elements of your vector is a vector (of length 1) itself. Use `⍴¨` to get some insight on that... – MBaas Jan 15 '19 at 10:01
  • I tried `{⍬ ≡ ⍴⍵}¨ v`, but it evaluates to a vector where all elements are 1. If I'm not mistaken, this implies that no element is a vector. – August Karlstrom Jan 15 '19 at 10:13
  • Where did that vector come from? If it was created by a fn, it might help to see the code.. – MBaas Jan 15 '19 at 10:16
  • Sorry, on the first try I didn't spot the offending element. The expression `({82 ≠ ⎕DR ⍵}¨ v) / ⍳⍴v` gave me the position (282), which holds a one-element vector with the integer zero. – August Karlstrom Jan 15 '19 at 10:36
  • Ok, thanks for finding that! I was really confused when you said it was all scalars ;-) – MBaas Jan 15 '19 at 10:37
  • Correction: `(({82 ≠ ⎕DR ⍵}¨ v) / ⍳⍴v) ⊃ v` is zero. – August Karlstrom Jan 15 '19 at 10:51
  • BTW, if you're using V17, you can do `⍸bool` instead of `bool/⍳⍴bool` ;-) – MBaas Jan 15 '19 at 13:22
  • 1
    @MBaas And so can you if you're using version 16.0. – Adám Jan 15 '19 at 13:34
2

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

mappo
  • 444
  • 2
  • 9
0

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

Adám
  • 6,573
  • 20
  • 37