View Full Version : Return Values
BoKirra
2009-03-19, 02:31 AM
Hi ALL,
I don't know how to do the following:
Say, if at the command prompt, the user enters
"L01", the routine returns "PipeLevel_01_Low".
If it was "L02", the routine returns "PipeLevel_01_Mid",
and so on...
Here is a list:
L01 = PipeLevel_01_Low
L02 = PipeLevel_01_Mid
L03 = PipeLevel_01_High
L04 = PipeLevel_02_Low
L05 = PipeLevel_02_Mid
L06 = PipeLevel_02_High
L07 = PipeLevel_03_Low
L08 = PipeLevel_03_Mid
L09 = PipeLevel_03_High
L10 = PipeLevel_04_Low
L11 = PipeLevel_04_Mid
L12 = PipeLevel_04_High
L13 = PipeLevel_05_Low
L14 = PipeLevel_05_Mid
L15 = PipeLevel_05_High
...
I guess it would be using a LIST function but am a year 1 pupil at LISP primary school. :Oops:
Again, your helps are much appreciated.
ccowgill
2009-03-19, 01:40 PM
Hi ALL,
I don't know how to do the following:
Say, if at the command prompt, the user enters
"L01", the routine returns "PipeLevel_01_Low".
If it was "L02", the routine returns "PipeLevel_01_Mid",
and so on...
Here is a list:
L01 = PipeLevel_01_Low
L02 = PipeLevel_01_Mid
L03 = PipeLevel_01_High
L04 = PipeLevel_02_Low
L05 = PipeLevel_02_Mid
L06 = PipeLevel_02_High
L07 = PipeLevel_03_Low
L08 = PipeLevel_03_Mid
L09 = PipeLevel_03_High
L10 = PipeLevel_04_Low
L11 = PipeLevel_04_Mid
L12 = PipeLevel_04_High
L13 = PipeLevel_05_Low
L14 = PipeLevel_05_Mid
L15 = PipeLevel_05_High
...I guess it would be using a LIST function but am a year 1 pupil at LISP primary school. :Oops:
Again, your helps are much appreciated.
use a condition statement.
(cond
((= input L01)
(setq name PipeLevel_01_low)
)
((= input L02)
(setq name PipeLevel_01_mid)
)
;...
)
rkmcswain
2009-03-19, 02:22 PM
Hi ALL,
I don't know how to do the following:
Say, if at the command prompt, the user enters
"L01", the routine returns "PipeLevel_01_Low".
If it was "L02", the routine returns "PipeLevel_01_Mid",
and so on...
Here is one way.
First create a list of the items, and then iterate this list until the result is found.
In the first function (finditem), the entire list is iterated, even if a match if found with the first item.
With a small list like you have this isn't a problem, but if you have thousands of items, you would probably want to stop searching as soon as you find a match. The second function (finditem2) does that. I'm sure these could be optimized further, but it should give you a starting point.
(setq mylist
(list
(list "L01" "PipeLevel_01_Low")
(list "L02" "PipeLevel_01_Mid")
(list "L03" "PipeLevel_01_High")
(list "L04" "PipeLevel_02_Low")
(list "L05" "PipeLevel_02_Mid")
(list "L06" "PipeLevel_02_High")
)
)
(defun finditem (lst x / item)
(foreach item lst
(if (eq (nth 0 item) x)
(setq res (nth 1 item))
)
)
res
)
(defun finditem2 (lst x / n p item)
(setq n 0 res nil)
(while (< n (setq p (length lst)))
(setq item (nth n lst))
(if (eq (nth 0 item) x)
(setq res (nth 1 item) n (1+ p))
)
(setq n (1+ n))
)
res
)
;;;EXAMPLES
_1_$ (finditem2 mylist "L02")
"PipeLevel_01_Mid"
_1_$ (finditem2 mylist "L03")
"PipeLevel_01_High"
_1_$ (finditem2 mylist "L01")
"PipeLevel_01_Low"
_1_$ (finditem2 mylist "L08")
nil
_1_$
RobertB
2009-03-19, 09:02 PM
Here is one way.
First create a list of the items...You were so close! :shock:
(setq masterList (list
(cons "L01" "PipeLevel_01_Low")
(cons "L02" "PipeLevel_01_Mid")
(cons "L03" "PipeLevel_01_High")
(cons "L04" "PipeLevel_02_Low")))
(cdr (assoc "L01" masterList))
rkmcswain
2009-03-19, 09:50 PM
You were so close! :shock:
Not.!!! :Oops:
BoKirra
2009-03-20, 02:22 AM
Thanks for everyone's help.
But I don't real get...
Here is my sample.
What is wrong?
(defun c:Display_PipeLevel (/ PipeLevel_ID PipeLevel_List PipeLevel)
(setq PipeLevel_ID
(getstring T "\nEnter a pipe level ID: "))
(setq PipeLevel_List
((L01 PipeLevel_01_Low)
(L02 PipeLevel_01_Mid)
(L03 PipeLevel_01_High)
(L04 PipeLevel_02_Low)
(L05 PipeLevel_02_Mid)
(L06 PipeLevel_02_High)
(L07 PipeLevel_03_Low)
(L08 PipeLevel_03_Mid)
(L09 PipeLevel_03_High)
(L10 PipeLevel_04_Low)
(L11 PipeLevel_04_Mid)
(L12 PipeLevel_04_High)
(L13 PipeLevel_05_Low)
(L14 PipeLevel_05_Mid)
(L15 PipeLevel_05_High)
)
); end of setq
(if (= PipeLevel_ID L01)
(setq PipeLevel
(cdr (assoc L01 PipeLevel_List)))
); end of if
(if (= PipeLevel_ID L02)
(setq PipeLevel
(cdr (assoc L02 PipeLevel_List)))
); end of if
;;; ...and so on
(prompt "\nThe level is PipeLevel.")
(princ)
); end of defun
Try the following changes...
(defun c:Display_PipeLevel (/ PipeLevel_ID PipeLevel_List PipeLevel)
(setq PipeLevel_ID
(getstring T "\nEnter a pipe level ID: "))
(setq PipeLevel_List
((cons "L01" "PipeLevel_01_Low")
(cons "L02" "PipeLevel_01_Mid")
(cons "L03" "PipeLevel_01_High")
(cons "L04" "PipeLevel_02_Low")
(cons "L05" "PipeLevel_02_Mid")
(cons "L06" "PipeLevel_02_High")
(cons "L07" "PipeLevel_03_Low")
(cons "L08" "PipeLevel_03_Mid")
(cons "L09" "PipeLevel_03_High")
(cons "L10" "PipeLevel_04_Low")
(cons "L11" "PipeLevel_04_Mid")
(cons "L12" "PipeLevel_04_High")
(cons "L13" "PipeLevel_05_Low")
(cons "L14" "PipeLevel_05_Mid")
(cons "L15" "PipeLevel_05_High")
)
); end of setq
(prompt (strcat "\nThe level is " (cdr (assoc PipeLevel_ID PipeLevel_List))"."))
(princ)
); end of defun
BoKirra
2009-03-20, 05:36 AM
Try the following changes...
(defun c:Display_PipeLevel (/ PipeLevel_ID PipeLevel_List PipeLevel)
(setq PipeLevel_ID
(getstring T "\nEnter a pipe level ID: "))
(setq PipeLevel_List
((cons "L01" "PipeLevel_01_Low")
(cons "L02" "PipeLevel_01_Mid")
(cons "L03" "PipeLevel_01_High")
(cons "L04" "PipeLevel_02_Low")
(cons "L05" "PipeLevel_02_Mid")
(cons "L06" "PipeLevel_02_High")
(cons "L07" "PipeLevel_03_Low")
(cons "L08" "PipeLevel_03_Mid")
(cons "L09" "PipeLevel_03_High")
(cons "L10" "PipeLevel_04_Low")
(cons "L11" "PipeLevel_04_Mid")
(cons "L12" "PipeLevel_04_High")
(cons "L13" "PipeLevel_05_Low")
(cons "L14" "PipeLevel_05_Mid")
(cons "L15" "PipeLevel_05_High")
)
); end of setq
(prompt (strcat "\nThe level is " (cdr (assoc PipeLevel_ID PipeLevel_List))"."))
(princ)
); end of defun
Thank you for your help.
But I get this ERROR message:
Command: Display_PipeLevel
Enter a pipe level ID: L01
; error: bad function: ("L01" . "PipeLevel_01_Low")
Thank you for your help.
But I get this ERROR message:
:Oops:
I forgot to change this line
((cons "L01" "PipeLevel_01_Low")
to this
(list
(cons "L01" "PipeLevel_01_Low")
BoKirra
2009-03-20, 05:58 AM
:Oops:
I forgot to change this line
((cons "L01" "PipeLevel_01_Low")
to this
(list
(cons "L01" "PipeLevel_01_Low")
Sorry, Opie.
I now get this:
Command: Display_PipeLevel
Enter a pipe level ID: l01
; error: bad argument type: stringp nil
irneb
2009-03-20, 07:25 AM
You might also want to try the following:(setq PipeLevel_ID
(strcase (getstring T "\nEnter a pipe level ID: ")))This'll ensure that whatever the user entered will be in UPPERCASE. Otherwise if the user enters something like "l02" the result willbe nil with the assoc.
BoKirra
2009-03-23, 02:18 AM
You might also want to try the following:(setq PipeLevel_ID
(strcase (getstring T "\nEnter a pipe level ID: ")))This'll ensure that whatever the user entered will be in UPPERCASE. Otherwise if the user enters something like "l02" the result willbe nil with the assoc.
Thanks, irneb.
It works perfectly.
Wonderful...
:Puffy:
irneb
2009-03-23, 06:37 AM
Glad I could help!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.