How to access multiple values ​​returned by a function (e.g. cl: parse-integer)?

I am trying to get three numbers from a string

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

Now this returns 2 , namely the number where it can be analyzed. Therefore I can now give

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

But how can I save the return value of 2 and 4 . If I setq into a variable, then only 12 and 3 are saved?

+6
source share
1 answer

Please read the "theory" here .

In short, you can bind 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 several 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 can simply do

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

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

PPS See also:

+11
source

Source: https://habr.com/ru/post/977625/


All Articles