The accepted idiom is

Base&#.^:_1 Value
.

This gives

   digits=.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
   Base=.#digits
   convert=.(digits {~ Base&#.^:_1)"0

You would have to do something just a little fancier to do convert without
rank 0; if it is run on a vector leading spaces will be filled with 'A'.

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Tracy Harms
Sent: Thursday, December 16, 2010 3:31 PM
To: Programming forum
Subject: [Jprogramming] increasing-width base conversion (e.g. Excel column
labels)

I encountered the question of how to produce the alphabetic label for a
given column in Excel. I enjoyed analyzing the problem, and I enjoyed coding
a solution in J.

I notice that I have difficulty "ironing out" the program I've written.
There is a need to use several calculated results more than once and I'm not
particularly happy with how these are structured in the program I've
written, especially the last half.

What I'd particularly like is to have the "central" calculation shine
through more visibly. I'm thinking about
  (howmany # Base) #: Value

Would anybody like to restructure this, or provide alternative solutions to
this problem?

--
Tracy



convert=: 3 : 0
  Base  =. # Digits=. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  offset=. [:+/ [^ [:>: i.@:]
  om    =. ( * 0<]) @ <. @ ^. NB. order of magnitude, in chosen base (x)
  OM    =. Base om y
  Value =. ( -  Base offset"0 OM"_ ) y
 ((OM + _1<Value) # &.> Base ) (Digits{~ #: )&> Value
)

NB. 'example execution
   convert (] , (24+]) , (700+])) i.4
A
B
C
D
Y
Z
AA
AB
ZY
ZZ
AAA
AAB
----------------------------------------------------------------------
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