Antonio

One option is to simply track the current index of your loop (since in
your example you are iterating over each item anyways) and do a
comparison of the current list element to the one you are searching for.

If you really wanted to use the member$ function you could try slicing
the list at point identified by the member$ function.  I have included
an example of one way to do this, given a search item and a list it
returns a list containing the positions the item is located.

Apologies if it isn't the most efficient or best practice, I haven't
touched Jess in a while, others may be able to provide you better
answers.

;;
;; Example of finding duplicate locations of an item in a list
;;

(deffunction findMember (?item ?list)
        "Finds and returns the positions of the specified search item
         in the specified multifield as a multifield (list)"
        (return (findMemberImpl ?item ?list 0 (create$)))
)

(deffunction findMemberImpl (?item ?list ?offset ?retList)
        "Recursive implementation that locates the search item then
         slices the list one position beyond the list.  Users should
         call findMember(item,list)"
        (bind ?position (member$ ?item ?list))
        (if (neq ?position FALSE)
                then
                        (bind ?offset (+ ?offset ?position))
                        (bind ?retList (create$ ?retList ?offset))
                        (findMemberImpl ?item
                                                        (subseq$ ?list (+ 
?offset 1) (length$ ?list))
                                                        ?offset
                                                        ?retList)
                else
                        (return ?retList)
        )
)

;;
;; Test it out
;;
(defglobal ?*a* = (create$ aa aa bb cc aa bb))

(printout t "Searching in list " ?*a* crlf)
(foreach ?f (union$ ?*a*)
        (printout t "Found " ?f " at " (findMember ?f ?*a*) crlf)
)

;;
;; End Sample
;;

This should yield an output like:
Searching in list (aa aa bb cc aa bb)
Found bb at (3 6)
Found aa at (1 2 4)
Found cc at (4)


Hope this helps,

--jason

On Thu, 2007-01-11 at 13:10 +0100, Antonino Lo Bue (gmail) wrote:
> Hi everyone,
> I've problems with list members position:
>  
>  (defglobal ?*a* =  (create$ aa aa))
>  thus I have the list: (aa aa)
>  
> I want to use member position, but if I write:
>  
>      (foreach ?f ?*a* (printout t (member$ ?f ?*a*)crlf ))
>  
> I obtain:
>      1
>      1
>  
> Otherwise I want:
>     1
>     2
>  
> This problem is because member$ function find the index of a element
> in list if this element exist in the list, thus if the element is
> duplicate in the list member$ function find the first element two
> times in the same position.
>  
> How can I obtain the position of an element in the list even if is a
> duplicate?
>  
> ------------------------------------------------------------------------
> Antonino Lo Bue
> Research Fellow
> ICAR-CNR Palermo
> Phone: 091-6809256
> Web: http://medialab.pa.icar.cnr.it/Personali/personali.html
> Email: [EMAIL PROTECTED]
>  

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to