Page 1 of 1

converting array to character matrix

Posted: Mon Feb 09, 2015 4:30 pm
by drothman
Veteran apl guy...not too much apl2...stuck on something like this..

given an nx2 array of scalars (in this case characters), i simply want to form a single character matrix where the strings are aligned in columns..something akin to:

∇q∇
∇ z←q
[1] z←⍉2 4⍴'one' 'two' 'three' 'four' 'green' 'red' 'blue' 'yellow'
[2] z←(↑z[;1]),(((1↑⍴z),10)⍴''),↑z[;2]

q
one green
two red
three blue
four yellow

i suspect there's some combination of APL2 constructs to get this done easily, but i haven't hit upon it. The example only contains 2 columns, but ideally i'd like to generalize it for n,m arrays.

I realize that ⍴↑z is 4 2 6 and i can dispense with the padded space by catinating along the second dimension (at the cost of not necessarily one space between the columns, but that's no prob), but that too isn't a general solution.

thanks for any help (just noticed that 'z' is not outputting on the screen correctly...think of z as two left justified excel columns)

Re: converting array to character matrix

Posted: Mon Feb 09, 2015 7:34 pm
by DanB|Dyalog
Do I understand that you want it to look like this?

Code: Select all

    ⍉2 4⍴'one' 'two' 'three' 'four' 'green' 'red' 'blue' 'yellow'
 one    green 
 two    red   
 three  blue   
 four   yellow

in that case all you would need to do is apply thorn (⍕) to it.
As in

Code: Select all

    ⍕⍉2 4⍴'one' 'two' 'three' 'four' 'green' 'red' 'blue' 'yellow'

If you don't like the 1st blank column you can always drop it with 0 1↓

Re: converting array to character matrix

Posted: Mon Feb 09, 2015 8:20 pm
by drothman
ugh...thanks. been trying every combination/permutation of ¨⊂⊃↑⍴⍉ monadically and dyadically that i could think of and other than kludgy hacks was getting nowhere. i didn't notice or see or remember reading that ⍕ would be the no-brainer solution...

Re: converting array to character matrix

Posted: Tue Feb 10, 2015 11:14 pm
by DanB|Dyalog
Yes, both thorn (⍕) and ⎕FMT will return "what you see" into "what you get".
The difference is that if the result is a vector thorn will return a vector whereas ⎕FMT will always return a matrix:

Code: Select all

      ⍴⍕⍳8
15
      ⍴⍕1 8⍴⍳8
1 15
      ⍴⎕FMT ⍳8
1 15