From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: string to list or string to array Date: Sun, 21 Oct 2012 13:16:37 +0200 Organization: Informatimago Message-ID: <87pq4c848a.fsf@kuiper.lan.informatimago.com> References: <62642868-f757-4115-a047-34c319a1c30f@o5g2000yqi.googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1350818414 19468 80.91.229.3 (21 Oct 2012 11:20:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Oct 2012 11:20:14 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 21 13:20:22 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TPtZt-0007yV-6D for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2012 13:20:21 +0200 Original-Received: from localhost ([::1]:59998 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TPtZl-0001AJ-MS for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2012 07:20:13 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 73 Original-X-Trace: individual.net 0bU1+rA9AfOnJTf1wCTq9gkUYW+nNqfAXAzVpsqHPeWkDMtKw7va6YAWGkodzyRu3C Cancel-Lock: sha1:YjhlZjZiMjNiN2UzNmIzNTkwMjUzYjI3OWQwYWQ1NGVkYmUxZjk2Zg== sha1:PLSQ18HenMZnh/t7m0IR8+WK3G4= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:195017 comp.emacs:102639 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:87346 Archived-At: Swami Tota Ram Shankar writes: > Friends, I am trying to read text from a file opened in emacs and > recognize it as array or list so that I can do some vector additions > using mapcar. > > I am mainly stuck at the stage of converting the string text to array > or list. > > The method which I use to locate the text is "looking-at" function and > the regexp works. > > (when (looking-at "[[0-9.+-\\ ]+]") ^^ 12 The character 1 has the meaning you'd want the character 2 to have. Your regexp parses as: one or more of any character in "[0123456789.+- " followed by "]". You want: (looking-at "\\[[0-9.+-\\ ]+\\]") The third backslash is optional, but I add it for clarity. > some code that is to play around for debugging and messaging and > checking the match > (forward-char (+ (string-width (match-string 0)) 1)) > (setq V (match-string 0)) > (setq V (intern (match-string 0))) > > > The cursor is placed on the start bracket, ie "[" > > and text looks like for example array of indefinite length > > [17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16] > > > Please suggest some different solutions that can take the string > matched, match-string for example and convert to a list or an array on > which I can do mapcars or lambdas. 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]) -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.