0

I would like to have this table

#+NAME: addition-table
|   |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
|---+----+----+----+----+----+----+----+----+----+----|
| 0 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| 1 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |
| 2 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 |
| 3 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
| 4 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 |
| 5 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 |
| 6 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 |
| 7 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 8 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 9 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
|---+----+----+----+----+----+----+----+----+----+----|

represented as a list. This is what I did:

#+BEGIN_SRC emacs-lisp 
'((0 00 01 02 03 04 05 06 07 08 09) 
(1 01 02 03 04 05 06 07 08 09 10)
(2 02 03 04 05 06 07 08 09 10 11) 
(3 03 04 05 06 07 08 09 10 11 12) 
(4 04 05 06 07 08 09 10 11 12 13) 
(5 05 06 07 08 09 10 11 12 13 14) 
(6 06 07 08 09 10 11 12 13 14 15) 
(7 07 08 09 10 11 12 13 14 15 16) 
(8 08 09 10 11 12 13 14 15 16 17) 
(9 09 10 11 12 13 14 15 16 17 18))
#+END_SRC

Unfortunately, upon evaluation, elisp wants to remove any leading zero, hence, I get this return:

#+RESULTS:
| 0 | 0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
| 1 | 1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 |
| 2 | 2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 |
| 3 | 3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 |
| 4 | 4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 |
| 5 | 5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 |
| 6 | 6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 7 | 7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 8 | 8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 9 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

It's no different in the scratch buffer, by the way.

((0 0 1 2 3 4 5 6 7 8 9) (1 1 2 3 4 5 6 7 8 9 10) (2 2 3 4 5 6 7 8 9 10 11) (3 3 4 5 6 7 8 9 10 11 12) (4 4 5 6 7 8 9 10 11 12 13) (5 5 6 7 8 9 10 11 12 13 14) (6 6 7 8 9 10 11 12 13 14 15) (7 7 8 9 10 11 12 13 14 15 16) (8 8 9 10 11 12 13 14 15 16 17) (9 9 10 11 12 13 14 15 16 17 18)) 

How can I tell elisp to keep my leading zeroes as in the original table?

1

You can use this org table formula, format is used to ensure the number's width is 2 and padded with zero, as @sds's answer suggested:

#+NAME: addition-table
|   |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
|---+----+----+----+----+----+----+----+----+----+----|
| 0 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| 1 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |
| 2 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 |
| 3 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
| 4 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 |
| 5 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 |
| 6 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 |
| 7 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 8 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 9 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
|---+----+----+----+----+----+----+----+----+----+----|
#+TBLFM: @2$2..@>$>='(format "%02d" (+ @1 $1));N

You can also quote these numbers to convert them into strings, for example, with query-replace-regexp:

C-M-% [0-9]+ → \,(format "\"%02d\"" \#&)

then

#+BEGIN_SRC emacs-lisp 
'(("00" "00" "01" "02" "03" "04" "05" "06" "07" "08" "09") 
("01" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10")
("02" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11") 
("03" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12") 
("04" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13") 
("05" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14") 
("06" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15") 
("07" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16") 
("08" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17") 
("09" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18"))
#+END_SRC

#+RESULTS:
| 00 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| 01 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |
| 02 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 |
| 03 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
| 04 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 |
| 05 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 |
| 06 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 |
| 07 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 08 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 09 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
  • In databases, MS Excel, and other places there is the concept of "alphanumeric" which can allow a field to not be interpreted as a number. For example, a person's social security number is actually not a number in the math sense, but an ID that just happens to be numeric. Declaring it alphanumeric precludes doing arithmetic on it. Apparently there is no elisp function that allows this. The closest we can get is by double-quoting. Correct? In my OP I wanted to go from org table to list. – 147pm Nov 9 at 15:55
1

TL;DR

You need to format your numbers with the leading zeros if you want them:

(format "%03d" 3)
==> "003"

Details

When you type

(defvar foo '(00 01 02))

and evaluate it, Emacs reads the string "01" and recognizes it as a number (integer) and stores it in the list. Then it promptly forgets how the number was represented - as "1" or "01" or "000001".

When the list is printed, Emacs cannot know how you want your numbers to be represented, so it prints them with the usual decimal form.

If you want leading zeros (or, say, octal representation), you need to specify that explicitly by calling format in a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.