unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Swami Tota Ram Shankar <tota_ram@india.com>
To: help-gnu-emacs@gnu.org
Subject: Re: string to list or string to array
Date: Sun, 21 Oct 2012 12:03:10 -0700 (PDT)	[thread overview]
Message-ID: <4e87f58c-72fc-457b-8596-c792c39472e5@m4g2000yqb.googlegroups.com> (raw)
In-Reply-To: 87pq4c848a.fsf@kuiper.lan.informatimago.com

On Oct 21, 4:16 am, "Pascal J. Bourguignon" <p...@informatimago.com>
wrote:
> Swami Tota Ram Shankar <tota_...@india.com> 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])
>

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.








  reply	other threads:[~2012-10-21 19:03 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-21 10:37 string to list or string to array Swami Tota Ram Shankar
2012-10-21 11:16 ` Pascal J. Bourguignon
2012-10-21 19:03   ` Swami Tota Ram Shankar [this message]
2012-10-21 20:22     ` Pascal J. Bourguignon
2012-10-23  1:16       ` Style Issues in Lisp and Scheme programming, setq versus let ... and onion structure with multiple cores or eyes or kernels " Swami Tota Ram Shankar
2012-10-23  2:36         ` PJ Weisberg
     [not found]         ` <mailman.11503.1350959781.855.help-gnu-emacs@gnu.org>
2012-10-23  3:02           ` Swami Tota Ram Shankar
2012-10-24 15:03             ` Stefan Monnier
2012-10-25  3:20               ` gnuist007
2012-10-25  8:25                 ` José A. Romero L.
2012-11-21 21:56                 ` Pascal J. Bourguignon
2012-10-23  8:15         ` Pascal J. Bourguignon
2012-10-24  8:05         ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4e87f58c-72fc-457b-8596-c792c39472e5@m4g2000yqb.googlegroups.com \
    --to=tota_ram@india.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).