Thanks Skip,
This is a total hack for your b case using fill.
]b=. 0 12 34 678 20 ,. 3 40 873 9 44
0 3
12 40
34 873
678 9
20 44
pad=: _3&({.!.'0')@":"0
pad1=:(;"1)@:($ $ ,@:(,&' 'each)@: ;:@: pad)
pad1 b
000 003
012 040
034 873
678 009
020 044
I think the 8!:2 conjunction is the way to go though, as it is able to change
widths and handle multiple columns
pad2=:('r<0>0.0' 8!:0 ])"1 &. |:
pad2 b
┌───┬────┐
│000│0003│
├───┼────┤
│012│0040│
├───┼────┤
│034│0873│
├───┼────┤
│678│0009│
├───┼────┤
│020│4400│
└───┴────┘
]c=. 0 12 34 678 20 ,. 3 40 873 9 4400,. 1 2 30 4 5
0 3 1
12 40 2
34 873 30
678 9 4
20 4400 5
pad2 c
┌───┬────┬──┐
│000│0003│01│
├───┼────┼──┤
│012│0040│02│
├───┼────┼──┤
│034│0873│30│
├───┼────┼──┤
│678│0009│04│
├───┼────┼──┤
│020│4400│05│
└───┴────┴──┘
;"1 @: (,&' ' each) pad2 c NB. break them out of their boxes this way
000 0003 01
012 0040 02
034 0873 30
678 0009 04
020 4400 05
I'm off to bed and expect Ric to show me how to do it without boxes tomorrow!
:-)
Cheers, bob
> On Feb 18, 2019, at 11:49 PM, 'Skip Cave' via Programming
> <[email protected]> wrote:
>
> Bob & Ric,
>
> Thanks! the pad verb does exactly what I specified. However, I have a more
> complex issue as well -
> padding a 2 dimensional array.
>
> ]b=. 0 12 34 678 20 ,. 3 40 873 9 44
>
> 0 3
>
> 12 40
>
> 34 873
>
> 678 9
>
> 20 44
>
>
> I need a pad1 verb that does this:
>
> pad1 b
>
> 000 003
>
> 012 040
>
> 034 873
>
> 678 009
>
> 020 044
>
>
> What if some columns have 2-digit maximum widths? 4-digit maximum widths?
>
> Is it possible to auto-pad to the maximum digit width in a column?
>
> Also, Is it possible to generalize this padding to n-dimensional arrays?
>
>
> Skip
>
>
> On Mon, Feb 18, 2019 at 10:14 PM Ric Sherlock <[email protected]> wrote:
>
>> Thanks Bob - nice!
>> I think you could avoid the boxing using:
>> pad=: _3&({.!.'0')@":"0
>>
>>
>> On Tue, Feb 19, 2019 at 4:39 PM 'robert therriault' via Programming <
>> [email protected]> wrote:
>>
>>> Hey Ric,
>>>
>>> I get a chance to repay the favour of helping me out on StackOverflow
>>> yesterday.
>>> This solution relies on some boxing, but it does use fill at its core.
>>>
>>> And yes Skip it does end up with a literal result.
>>> I don't know of a way around that, but I would be happy to be proved
>> wrong
>>> as well!
>>>
>>> a
>>> 0 12 34 678 20
>>> pad=: > @ (_3&({.!.'0') @ ": each)
>>> pad a
>>> 000
>>> 012
>>> 034
>>> 678
>>> 020
>>>
>>> Cheers, bob
>>>
>>>> On Feb 18, 2019, at 6:00 PM, Ric Sherlock <[email protected]> wrote:
>>>>
>>>> ('r<0>0.0' 8!:2 ]) ,. a
>>>
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm