7

I'm trying to get three numbers out of a string

(parse-integer "12 3 6" :start 0 :junk-allowed t)
12 ;
2

Now this returns 2 as well, which is the number where it could be parsed. So I can now give

(parse-integer "12 3 6" :start 2 :junk-allowed t)
3 ;
4

But how do I store the value of 2 and 4 that it returned. If I setq it into a variable only the 12 and 3 are stored?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
shrinidhisondur
  • 771
  • 1
  • 8
  • 18

1 Answers1

11

Please read the "theory" here.

Briefly, you can bind the multiple values with multiple-value-bind:

(multiple-value-bind (val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t)
  (list val pos))
==> (12 2)

You can also setf multiple values:

(setf (values val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t))
val ==> 12
pos ==> 2

See also VALUES Forms as Places.

PS. In your particular case, you might just do

(read-from-string (concatenate 'string 
                               "("
                               "12 3 6"
                               ")"))

and get the list (12 3 6). This is not the most efficient way though (because it allocates unnecessary memory).

PPS See also:

  1. How to convert a string to list using clisp?
  2. In lisp, how do I use the second value that the floor function returns?
Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278