PDA

View Full Version : help required string comparison add2list


VINIT
01-23-2006, 12:46 AM
Dear friends,

I have two lists. both the lists contain strings.

Part names list =>
(setq partnames (list "/safetyvalve/spring-guide-2"
"/safetyvalve/spring-guide-1"
"/safetyvalve/adjustbolt" "/safetyvalve/spindle" "/safetyvalve/cover"
"/safetyvalve/plug" "/safetyvalve/blowdownring" "/safetyvalve/seat"
"/safetyvalve/body" "/safetyvalve/spring"))

Viewset names list =>
(setq viewnames (list "/safetyvalve/body/vs1" "/safetyvalve/seat/vs1")

I wrote a function which accepts two parameters, first as string and second as a list . This function uses common lisp "search" function and returns the element from list whose value contains the string passed as first argument.

(defun itemExist(strItem varstringList)
(dolist (i varstringList)
(if (search strItem i)
i
nil
)
)
)

Considering above "viewnames" variable as list of strings,
Usage example: (print (itemExist "/safetyvalve/body" viewnames))
Should return "/safetyvalve/body/vs1"

I want to use the this function for all elements in partlist and compare every element of partlist to that of viewnames list. here's how I intend to use in my code.

;Loop for all parts and find their related viewset names
(dolist (part plist)
(if (itemExist part viewnames)
(setq part_n_view (list part (itemExist part viewnames)))
(setq part_n_view (list part "Not found"))
);if
);dolist

My problem is, it doesn't return the element although it is present in list. Could anyone please correct the problem if I am wrong.

Regards,
Vinit

Markus
01-23-2006, 10:14 PM
The problem is in your function itemExist.
You must return i if you found a match.
Correct would be:


(defun itemExist(strItem varstringList)
(dolist (i varstringList)
(when (search strItem i)
(return-from itemExist i)
)
)
nil
)


The drawback of using search is that this function is not extended (multi-byte) string safe. If this is important for your program, please consider rewriting it using sd-string-match-pattern-p.

Markus