A more compact variant, in case anyone wants that (though, granted,
without safety measures):

   d12a=: 0 1}.]":@+10x^[
   4 d12a a
0001
0002
0003
0010
0020
0030
0100
0200
0300
0400
1000
2000
3000

A "safer" version would be:
   d12b=: -@[{."1]":@+10x^[

(Same result, for this example, but properly truncates extra digits in
right argument.)

(Note also that you'll need an x in your numeric argument if it
exceeds 1.8e19 by much -- or I think this limit might have been 9e15
in older versions of J...  Anyways, d12b could be made to be "safer"
(closer to accurate) for arguments which exceed this limit, but that
could only hide part of the error.)

And... d12a/d12b use nines complement representation for negative numbers.

--
Raul

On Mon, Feb 4, 2019 at 10:20 PM Don Kelly <d...@shaw.ca> wrote:
>
> Phrases 13C gives
>
> d12=: [:}."1[:": (10"_^[)+([:,.])
>
> Format list y as x-wide col with leading 0s
>
>    a=: 1 2 3 10 20 30 100 200 300 400 1000 2000 3000
>    4 d12 a
> 0001
> 0002
> 0003
> 0010
> 0020
> 0030
> 0100
> 0200
> 0300
> 0400
> 1000
> 2000
> 3000
>
> The column form serves well in making a number list for tickets using
> clipwrite  to put in word or writer.
>
>
> Don Kelly
>
> On 2019-02-03 11:15 a.m., 'Skip Cave' via Programming wrote:
> > Paolo and Devon provided the named dyadic verbs Fill and lead0s that
> > perform the function I specified. Devon's verb has some extra bells and
> > whistles which I like.
> > Roger pointed out that the built-in format function 8!:2 allows a fill
> > option, but with my limited J expertise I wasn't able to turn Roger's
> > example into a named dyadic verb.
> > I liked R.E. Boss' approach, as it automatically sets the number of
> > characters, given the number of characters in the largest integer, as that
> > would work for my current problem. Again, however, I don't know how to turn
> > that verb into a named monadic verb.
> > All in all, I learned quite a lot from this exercise.
> >
> > Skip
> >
> >
> > On Sun, Feb 3, 2019 at 2:36 AM Skip Cave <s...@caveconsulting.com> wrote:
> >
> >> What's the best way to add leading zeros to a set of integers. How to
> >> design a verb f, that does the following:
> >>
> >>     a =.  1 2 3 4 5 10 20 30 40 50 100 200 300 400 500 1000 2000 3000 4000
> >> 5000
> >> 4 f a NB. make each integer a total of 4 digits using leading zeros.
> >> 0001 0002 0003 0004 0005 0010 0020 0030 0040 0050 0100 0200 0300 0400 0500
> >> 1000 2000 3000 4000 5000
> >>
> >> Skip
> >>
> >>
> > ----------------------------------------------------------------------
> > 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

Reply via email to