List Manuipulation

List Manuipulation

Moshe-A
Mentor Mentor
1,576 Views
11 Replies
Message 1 of 12

List Manuipulation

Moshe-A
Mentor
Mentor

and i have another issue

 

have the list (setq selected^ '(0 5 9 12 18))

and have list of strings (setq layouts^ '("sheet a0" "sheet a1" "sheet a2"))

 

i looking for the most efficient way to turn it => "sheet a0,sheet a1, sheet a2"

 

what i tried is this:

 

(defun concat (item)

 (strcat (nth item layouts^) ",")

)

 

(apply 'concat selected^)

 

and i'm getting this error

error: too many arguments

 

why this will work => (apply '+ selected^) returns 44

but my defined concat does not? what's the different?

 

again looking for the most efficient way to do it.

 

thanks

Moshe

 

 

0 Likes
Accepted solutions (1)
1,577 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

 

Depending on what or how you are planning to use the result

 

(substr (apply 'strcat
	       (mapcar '(lambda (a)
		   (strcat "," a)) layouts^)) 2)
"sheet a0,sheet a1,sheet a2"

 

 What does the value for selected^  referencing to?

'(0 5 9 12 18)

How is it related to layouts^ variable?

 

 

0 Likes
Message 3 of 12

Moshe-A
Mentor
Mentor

the selected^ list are the selected list_box items from the previous thread

 

 

 

0 Likes
Message 4 of 12

Moshe-A
Mentor
Mentor

i sorry my mistake

 

lets' say the selected is:

(setq selected^ '(0 2 4))

 

and layouts:

(setq layouts '("sheet a0" "sheet a1" "sheet a2" "sheet a3" "sheet a4"))

 

the result should be:  "sheet a0,sheet a2,sheet a4"

 

 

 

 

0 Likes
Message 5 of 12

pbejse
Mentor
Mentor

@Moshe-A wrote:

 

..why this will work => (apply '+ selected^) returns 44

but my defined concat does not? what's the different?

 

  


(apply '+ selected^)
	0 + 5 + 9 +  12 + 18

while

 

(apply 'concat selected^)
"sheet a0"
	(strcat
	  	(nth "sheet a0" layouts^) ",")
"sheet a1" (strcat (nth "sheet a1" layouts^) ",") "sheet a2" (strcat (nth "sheet a2" layouts^) ",")

Which will crash because function nth is expecting an integer.

 

 

 

(defun concat (item) (strcat item ","))
(mapcar 'concat layouts^)

will give you

 

 

("sheet a0," "sheet a1," "sheet a2,")

then

(apply 'strcat '("sheet a0," "sheet a1," "sheet a2,"))

will give you

"sheet a0,sheet a1,sheet a2," 

 

 

 

 

0 Likes
Message 6 of 12

Moshe-A
Mentor
Mentor
Accepted solution

OK figure it out

 

(substr  (apply  'strcat (mapcar '(lambda (item) (strcat "," (nth item layouts^))) selected^)) 2)

 

thanks mate

 

 

 

0 Likes
Message 7 of 12

pbejse
Mentor
Mentor

FWIW: For  your original code

 

(defun concat (item)
	(strcat "," (nth item layouts) )
)
       
(substr	(apply 'strcat
	       (mapcar 'concat
				(read (Strcat "(" selected^ ")"))) 
		)
	2
)

 

0 Likes
Message 8 of 12

Moshe-A
Mentor
Mentor

pbejse,

 

that ok but i was refering to apply function, i seems to me that apply function accepts only a build-in autolisp functions not a custom by defun (although it's wrriten in the document)  am i right?

 

Signature

(apply 'function list)
'function

Type: Symbol

A function.

The function argument can be either a symbol identifying a defun, or a lambda expression.

list

Type: List or nil

A list.

If the function accepts no arguments, the value can be nil.

 

 

0 Likes
Message 9 of 12

pbejse
Mentor
Mentor

@Moshe-A wrote:

functions not a custom by defun (although it's wrriten in the document)  am i right?


 

Not really, depending on how you use the function

 

(setq selected^ '(0 2 4))
(setq layouts '("sheet a0" "sheet a1" "sheet a2" "sheet a3" "sheet a4"))
  
(Defun concat (lst)
    	(mapcar '(lambda  (n)
		   (Strcat "," (nth n lst)) ) selected^)
    )
  
(substr
(apply 'strcat
(apply 'concat (list layouts))
)
2
)

Which is an odd way of concatenating the strings

 

 

 

0 Likes
Message 10 of 12

Moshe-A
Mentor
Mentor

and what this means from 'apply point of view'...if you use a defun symbol then the list argument can be a list with only 1 item in?!

that the meaning on doing: (apply 'concat (list layouts))

 

cause layouts is already a list - right?!

and this does works (apply 'strcat layouts)  - right?!

then why this won't work? 

(apply 'concat layouts)

 

maybe other has explanation to this?

 

 

 

 

 

 

 

0 Likes
Message 11 of 12

cadffm
Consultant
Consultant

 >returns 44 but my defined concat does not? what's the different?

The answer is more simple than funny.

 

"error: too many arguments"

It is easy and you know it: A function expects X arguments and you send more or less arguments.

Your concat expects  ONE argument, but you pass  5 arguments! (five item in selected^ list)

 

 A fine working example:

(setq selected^ '(0 2 4))
(setq layouts^ '("sheet a0" "sheet a1" "sheet a2" "sheet a3" "sheet a4" "sheet a5"))

(defun concat (a1 a2 a3) ;; <- 3 Arguments wanted
 (strcat (nth a1 layouts^) "," (nth a2 layouts^) "," (nth a3 layouts^))
)

(apply 'concat selected^) ;; 3 Arguments given
"sheet a0,sheet a2,sheet a4"

 

Thats how apply works, it is not iterate thru a list like foreach ore mapcar.

" i seems to me that apply function accepts only a build-in autolisp functions not a custom by defun (although it's wrriten in the document)  am i right?"

No .

 
Apply works like all other your own function!

Apply is not the problem, the difference is between functions like + - strcat ... and your own functions.

The difference is: This functions allows a dynamic length of argument list!

You are (with lisp) not able to define a function which allows a dynamic length of arguments.

 

So Apply is useless for using own functions within.

Sebastian

Message 12 of 12

Moshe-A
Mentor
Mentor

Ok Sebastian

now it is clear

 

it's pity the documentation fail to explain it this way

 

 

 

0 Likes