I want to replace the first 104 digits as it follows:
1 --> a
...
26 --> z
27 --> aa
...
52 --> az
53 --> ba
...
104 --> bz
I have tried as it follows:
(defun digits-to-letters ()
(interactive)
(goto-char (point-min))
(save-excursion
(save-restriction
(narrow-to-region (search-forward "\\title" nil t)
(search-forward "\\abstract" nil t))
(goto-char (point-min))
(while (search-forward-regexp "\\([0-9]+\\)" nil t)
(setq digit (match-string 0))
;; 1-26 to a-z
(if (< digit 27)
(perform-replace digit
`((lambda (data count)
(string (+ (1- ?a) digit))))
t t nil 1 nil (point-min) (point-max))
;; 27-52 to aa-az
(if (and (> digit 26)
(< digit 53))
(perform-replace digit
`((lambda (data count)
(string (+ (1- ?a) (concat "a"
digit)))))
t t nil 1 nil (point-min) (point-max))
;; 53-104 to ba-bz
(if (and (> digit 52)
(< digit 105))
(perform-replace "\\([0-9]+\\)"
`((lambda (data count)
(string (+ (1- ?a) (concat "b"
digit)))))
t t nil 1 nil (point-min) (point-max))
)))
))))
It gives me the following error:
Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p #("1" 0 1 (fontified t)))
P.S. I have just asked something like this, but for the first 26 digits: How to replace a digit by a letter?.
Updates
There is a mistake in the correspodence between digits and letters:
53 --> ba
...
78 --> bz
79 --> ca
...
104 --> cz
I have corrected my code as it follows:
(defun digits-to-letters ()
(interactive)
(goto-char (point-min))
(save-excursion
(save-restriction
(narrow-to-region (search-forward "\\title" nil t)
(search-forward "\\abstract" nil t))
(goto-char (point-min))
(while (search-forward-regexp "\\([0-9]+\\)" nil t)
(setq digit (string-to-number (match-string 0)))
(setq sdigit (match-string 0))
;; 1-26 to a-z
(if (< digit 27)
(replace-match (string (+ (1- ?a) digit)))
;; 27-52 to aa-az
(if (and (> digit 26)
(< digit 53))
(replace-match (concat
"a"
(string (+ (1- ?a) (- digit 26)))))
;; 53-78 to ba-bz
(if (and (> digit 52)
(< digit 79))
(replace-match (concat
"b"
(string (+ (1- ?a) (- digit 52)))))
;; 79-104 to ba-bz
(if (and (> digit 78)
(< digit 105))
(replace-match (concat
"c"
(string (+ (1- ?a) (- digit 78)))))
))))
))))
Now it works.
digit
to(match-string 0)
and compare that string in(< digit 27)
with a number. Maybe you want to use(setq digit (string-to-number (match-string 0)))
. Do you really want to useperform-replace
? You also usedigit
later on as string to replace. So you need both the numeric value and the string. Maybe introduce one more variable for that.