From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Swami Tota Ram Shankar Newsgroups: gmane.emacs.help Subject: Re: string to list or string to array Date: Sun, 21 Oct 2012 12:03:10 -0700 (PDT) Message-ID: <4e87f58c-72fc-457b-8596-c792c39472e5@m4g2000yqb.googlegroups.com> References: <62642868-f757-4115-a047-34c319a1c30f@o5g2000yqi.googlegroups.com> <87pq4c848a.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1350846314 7137 80.91.229.3 (21 Oct 2012 19:05:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Oct 2012 19:05: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 21:05: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 1TQ0ps-0003Ra-V2 for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2012 21:05:21 +0200 Original-Received: from localhost ([::1]:60591 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TQ0pl-0001Fe-1y for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2012 15:05:13 -0400 Original-Received: by 10.224.223.14 with SMTP id ii14mr4470073qab.3.1350846191196; Sun, 21 Oct 2012 12:03:11 -0700 (PDT) Original-Received: by 10.229.77.149 with SMTP id g21mr335086qck.1.1350846191154; Sun, 21 Oct 2012 12:03:11 -0700 (PDT) Original-Path: usenet.stanford.edu!x14no3588747qar.0!news-out.google.com!r17ni43198318qap.0!nntp.google.com!x14no3588738qar.0!postnews.google.com!m4g2000yqb.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Complaints-To: groups-abuse@google.com Injection-Info: m4g2000yqb.googlegroups.com; posting-host=99.61.86.51; posting-account=dlefMQoAAABzowG6c0cULB8igkwPchCd Original-NNTP-Posting-Host: 99.61.86.51 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0,gzip(gfe) Injection-Date: Sun, 21 Oct 2012 19:03:11 +0000 Original-Xref: usenet.stanford.edu gnu.emacs.help:195018 comp.emacs:102641 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:87347 Archived-At: On Oct 21, 4:16=A0am, "Pascal J. Bourguignon" wrote: > 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.+-\\ ]+]") > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0^^ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A012 > > The character 1 has the meaning you'd want the character 2 to have. > > Your regexp parses as: > > =A0 =A0 =A0one or more of any character in "[0123456789.+- " > =A0 =A0 =A0followed by "]". > > You want: > > =A0 =A0 =A0 (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 () > =A0 =A0(let ((vectors '())) > =A0 =A0 =A0(goto-char (point-min)) > =A0 =A0 =A0(while (re-search-forward "\\[[0-9.+-\\ ]+\\]" nil t) > =A0 =A0 =A0 =A0 (goto-char (match-beginning 0)) > =A0 =A0 =A0 =A0 (push (read (current-buffer)) vectors)) > =A0 =A0 =A0(nreverse vectors))) > > ;; [1 2 3.0] [4 5 6] > ;; [7 +8 -9] > > (get-vectors-from-buffer) > --> ([[0-9\.+-\\] +] > =A0 =A0 =A0[[0-9\.+-\\] +\\] > =A0 =A0 =A0[17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16] > =A0 =A0 =A0[[0-9\.+-\\] +\\] > =A0 =A0 =A0[1 2 3.0] > =A0 =A0 =A0[4 5 6] > =A0 =A0 =A0[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)))) ;(setq V (read-from-string "[17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16]" )) ; (insert (match-string 0)) ; print the entire matched string ;(insert (format "%s" V)) (insert (format "%s" (car (split-string (format "%s" V) " ")))) ;; (setq X (make-vector ;; (/ (length V ) 2) ;; 3 ) ) [17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16] I am using read-from-string and I need to get it to work. but it gives error Debugger entered--Lisp error: (wrong-type-argument stringp \[17\.16\ -17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\]) read-from-string(\[17\.16\ -17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\ 17\.16\]) eval((read-from-string (intern (match-string 0)))) eval-last-sexp-1(nil) eval-last-sexp(nil) * call-interactively(eval-last-sexp) If I dont use intern, I get some small little thing. (read-from-string "[17.16 -17.16 17.16 17.16 17.16 17.16 17.16 -17.16]" )) ([17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16] . 51) so I can car it. should I be using match-string or data-match or match-data to most simply get it working while using "looking-at" ? so the parsing works which a hard wired string as in the sequential execution below, but the problem is that its not working with match- string. (setq V (car (read-from-string "[17.16 -17.16 17.16 17.16 17.16 17.16 17.16 -17.16]" ))) (mapcar* (lambda(x y)(+ x y)) V V) Thanks.