Code Red > AutoLISP (Vanilla / Visual)

ByRef in lisp?

(1/2) > >>

mkweaver:
I know I must be missing something easy here:

Given two lists of data:
(setq List1 '(1 2 3 4 5))
(1 2 3 4 5)
(setq list2 '(a b c d e))
(A B C D E)

And a defun:
(defun test(Data)
  (subst (car datalist) "12" datalist)
 )

How can I change the defun, or specify the argument when calling the function, so that the function modifies the original(global) datalist rather than the local copy?  I know I could leave the argument list global, but I want to be able to work on either datalist with the same defun.

I've tried the following to no avail:

(test 'list1)
nil
list1
(1 2 3 4 5)

and

(test list1)
nil
list1
(1 2 3 4 5)

I think I must be missing something simple/basic here.

Suggestions are most welcome.

Mike Weaver

MP:
I just woke up, so common sense is not preventing me from posting this quick and dirty --


--- Code: ---(defun ByRef ( quotedVar / value )

    ;;  minimalist coding, no error checking (read "not good for production code")

    (setq value (eval quotedVar))

    (set quotedVar
   
        (subst "woot" (car value) value)       
   
    )

)
--- End code ---

Example:


--- Code: ---(setq lst '( "phee" "phi" "pho"))

(ByRef 'lst)

(princ lst)
--- End code ---

Having posted it let me say there are far better strategies for achieving what you want but I don't have the time to detail right now. I'm sure the thread will flourish in my absence. Cheers.

:)

mkweaver:

--- Quote from: MP on October 03, 2007, 08:56:41 AM ---I just woke up, so common sense is preventing me from posting this quick and dirty --

...Snip...

Having posted it let me say there are better strategies for achieving what you want but I don't have the time to detail right now. I'm sure the thread will flourish in my absence. Cheers.

:)

--- End quote ---

Thanks for the quick reply.  I will try to get my head wrapped around this and watch for additional replies.

Thanks,
Mike Weaver

CAB:
Mike,
Using your example routine it would work like this:

--- Code: ---(setq List1 '(1 2 3 4 5))
(setq list2 '(a b c d e))


(defun test(Data / value)
  (setq value (eval data))
  ;;  subst the first item in the list with "12"
  (set Data (subst "12" (car value) value))
  (princ)
)
--- End code ---

Command:
Command: (test 'list1)

Command: !list1
("12" 2 3 4 5)

Command: (test 'list2)

Command: !list2
("12" B C D E)


Although I don't understand why you want to do this?

gile:
Hi perhaps I don't understand, but, why do you (MP & CAB) store the data value in a local variable ?

The result returned by (subst ...) won't change the original Data and the (eval ...) expression needs a quoted variable and don't allows to directly do :
(test '(1 2 3 4 5))


--- Code: ---(defun test (data)
  (subst "12" (car data) data)
)
--- End code ---

$ (setq list1 '(1 2 3 4 5))
(1 2 3 4 5)
_$ (test list1)
("12" 2 3 4 5)
_$ (test '(1 2 3 4 5))
("12" 2 3 4 5)
_$ list1
(1 2 3 4 5)

Navigation

[0] Message Index

[#] Next page

Go to full version