unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION **
@ 2002-09-16  3:30 gnuist
  2002-09-16 13:07 ` D. Goel
  2002-09-17 17:49 ` Barry Margolin
  0 siblings, 2 replies; 3+ messages in thread
From: gnuist @ 2002-09-16  3:30 UTC (permalink / raw)


My problem is very simple for an emacs guru. More than one solution is
very welcome.

I have a list of numbers in a file as follows:

ABC98789
DDE90898889
FRE9090909

that is, first three letters and then a string of numbers and nothing else.

I want to write a lisp function (not a macro) that can read the first
three letter substring into a variable and the rest of the substring into
another variable. Then I want to use these substrings to generate my
final string. I know that can be done using the "insert" command. The problem
is how to put the text strings in a file into a variable? I know setq can
do this but then if I construct the string in sexp such as:

(setq letters-variable "ABC")

how do I eval it? I have tried eval-last-sexp IN THE LISP FUNCTION
and it gives some strange result in the minibuffer when the function is run.

Any and all help is appreciated.

With a macro using C-k and yank this is trivial but the kill buffer can only
hold (memorize) one piece at a time not the two pieces. Still it can be done
by moving to the two lines where they are separately held, but it is not a 
readible solution. It is at the turing machine level of copy and erase one
at a time, not store in named variables.

Once I have written this function for one string, I can run it on the 
whole list by C-u 3 M-x my-function.

Cheers

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION **
  2002-09-16  3:30 ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION ** gnuist
@ 2002-09-16 13:07 ` D. Goel
  2002-09-17 17:49 ` Barry Margolin
  1 sibling, 0 replies; 3+ messages in thread
From: D. Goel @ 2002-09-16 13:07 UTC (permalink / raw)


<newsgroups restricted to g.e.help>

> My problem is very simple for an emacs guru. More than one solution is
> very welcome.
> 
> I have a list of numbers in a file as follows:
> 
> ABC98789
> DDE90898889
> FRE9090909

;; returns somethign like (("ABC" "DEF" "GGH") ("1123" "223" "332"))
(defun gnuist-contents (file)
  ""
  (let (let-num let num
		(letters nil)
		(numbers nil))
    (find-file file)
    (goto-char (point-min))
    (while (forward-word 1)
      (setq let-num (format "%S" (sexp-at-point)))
      (setq let (substring let-num 0 3))
      (setq num (substring let-num 3 (length let-num)))
      (push let letters)
      (push num numbers))
    (list (reverse letters) (reverse numbers))))

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION **
  2002-09-16  3:30 ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION ** gnuist
  2002-09-16 13:07 ` D. Goel
@ 2002-09-17 17:49 ` Barry Margolin
  1 sibling, 0 replies; 3+ messages in thread
From: Barry Margolin @ 2002-09-17 17:49 UTC (permalink / raw)


In article <9e8ebeb2.0209151930.79f76ae9@posting.google.com>,
gnuist <gnuist007@hotmail.com> wrote:
>My problem is very simple for an emacs guru. More than one solution is
>very welcome.
>
>I have a list of numbers in a file as follows:
>
>ABC98789
>DDE90898889
>FRE9090909
>
>that is, first three letters and then a string of numbers and nothing else.
>
>I want to write a lisp function (not a macro) that can read the first
>three letter substring into a variable and the rest of the substring into
>another variable. Then I want to use these substrings to generate my
>final string. I know that can be done using the "insert" command. The problem
>is how to put the text strings in a file into a variable? I know setq can
>do this but then if I construct the string in sexp such as:
>
>(setq letters-variable "ABC")

(defun my-function (string)
  (let ((letters (substring str 0 3))
        (numbers (substring str 3)))
    (insert (format "The letters are %s, the numbers are %s."
                    letters numbers))))

>how do I eval it? I have tried eval-last-sexp IN THE LISP FUNCTION
>and it gives some strange result in the minibuffer when the function is run.

The minibuffer will show the return value of the expression.  It would help
if you described what you expected to happen and what actually happened,
rather than the vague "some strange result".

It seems like you need to learn Lisp.  The Emacs Lisp manual should be a
good place to start.

-- 
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-09-17 17:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-16  3:30 ** Q: HOW TO MANIPULATE STRINGS IN A FILE WITH A LISP FUNCTION ** gnuist
2002-09-16 13:07 ` D. Goel
2002-09-17 17:49 ` Barry Margolin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).