help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string to list or string to array


From: Pascal J. Bourguignon
Subject: Re: string to list or string to array
Date: Sun, 21 Oct 2012 22:22:40 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

Swami Tota Ram Shankar <address@hidden> writes:

> On Oct 21, 4:16 am, "Pascal J. Bourguignon" <address@hidden>
> wrote:
>> Since the syntax of this text is exactly the printed representation of
>> an emacs lisp vector, you can read it directly with read.
>>
>> (defun get-vectors-from-buffer ()
>>    (let ((vectors '()))
>>      (goto-char (point-min))
>>      (while (re-search-forward "\\[[0-9.+-\\ ]+\\]" nil t)
>>         (goto-char (match-beginning 0))
>>         (push (read (current-buffer)) vectors))
>>      (nreverse vectors)))
>>
>> ;; [1 2 3.0] [4 5 6]
>> ;; [7 +8 -9]
>>
>> (get-vectors-from-buffer)
>> --> ([[0-9\.+-\\] +]
>>      [[0-9\.+-\\] +\\]
>>      [17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16]
>>      [[0-9\.+-\\] +\\]
>>      [1 2 3.0]
>>      [4 5 6]
>>      [7 8 -9])
>>
>
> Hi Pascal, Thanks for the reply but there are some problems. I
> understood your change to regexp.
>
> However, you are doing too much in your and I need only read one
> string at a specific location at a time, not the whole buffer.
>
> Thus, I am having difficulty and need help. Let me write the modified
> story again.
>
> (when (looking-at "\\[[0-9.+-\\ ]+\\]")
> (forward-char (+ (string-width (match-string 0)) 1))
> (setq V (match-string 0))
> (setq V (read-from-string (intern (match-string 0))))

0- What's the purpose of moving the point?
1- Don't use setq, use let.
2- Don't bind two different things to the same variable!
3- read-from-string reads from a string, not a symbol, so why are you
   interning a symbol?

Let me advise you to read:

          An Introduction to Programming in Emacs Lisp
          http://www.gnu.org/software/emacs/emacs-lisp-intro/  or  M-: (info 
"(eintr)Top") RET




   (defun get-vector-at-point ()
     (when (looking-at "\\[[0-9.+-\\ ]+\\]")
       (goto-char (match-beginning 0))
       (read (current-buffer))))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]