Welcome to the COE Discussion Forum! 

 

To participate in the discussion forum, you must be logged in to the website.  If you forget your login information, please contact COE Headquarters at coe@coe.org or (800) 263-2255.

If you are new to the COE Discussion Forum and would like to participate, please register.


Register Today
COE 2009 Annual PLM Conference & TechniFair

COE DISCUSSION FORUM
Subject: List.IndexOf(,) Syntax

You are not authorized to post a reply.   
Author Messages
Tom08

11 Nov 2008 09:04 AM

I have created a small list and am attempting to use the "IndexOf" function to see if a specific item is in the list, and if so, at what position.  The documentation states the following:

List->IndexOf (Element: ObjectType, StartIndex:Integer):Integer
Returns the first index of a list item. The item is searched for from the start index.

No matter what I do, it seems to always return 0.  Here is the code I'm testing.

let L (List)
L = List("Item1","Item2","Item3","Item4" )
Message("List Size: " + L.Size())
Message("Get Item 3: " + L.GetItem(3))
Message("IndexOf 'Item2': " + L.IndexOf("Item2",0))

Can anyone offer any suggestions on what I'm doing incorrectly?  Thanks!

MBERRY

11 Nov 2008 10:06 AM
Tom08,

I think you have to pass that function an object. In your case, the quoted strings are not any kind of object in the part. I think it is doing object comparison on the object you pass in against each object in the list - if there is a match, it returns the index. If you make 4 string parameters and do a lookup on the second one, it works.

Mike

let L(List)
let A(String)
let B(String)
let C(String)
let D(String)

A = "Item1"
B = "Item2"
C = "Item3"
D = "Item4"

L = List(A, B, C, D)

Message("IndexOf Item2: " + L.IndexOf(B,1))
Tom08

11 Nov 2008 10:57 AM

You are correct, that works perfectly.    Thank you!

Tom08

11 Nov 2008 06:27 PM

Apparently this is trickier than I expected.  Any idea why this won't work?

let L(List)
let s(String)
let i (Integer)

L = List("40mm","50mm","75mm","100mm","150mm" )

s = "75mm"

i = L.IndexOf(s,0)

Message("Index of " + s + ": " + i)

 

Ultimately I'm trying to get to this (which doesn't work either):

/* P is an already existing parameter with multiple values.  It has been populated with the following line: */
/* P.AuthorizedValues = List("40mm","50mm","75mm","100mm","150mm" ) */

let L (List)
let s (String)
let i (Integer)

L = P.AuthorizedValues    /* Load all the values from P into the list L - This part is working ok as far as I can tell */

s = P    /* Set s equal to the current value of P - This part works fine as well */

i = L.IndexOf(s,0)    /* Find what position the current value of P (s) is in the list of possible values for P */
 
Message("Index of " + s + ": " + i)

MBERRY

12 Nov 2008 12:01 PM
Tom,

I see what you are trying to do, but IndexOf isn't really the way to do it.
Try something like this...

Mike

Let L(List)
Let i(Integer)
Let s(String)

L = P.AuthorizedValues
i = 0
for s inside L
{ if s == P Message("","Index of " + s + ": " +1)
i = i +1
}
Tom08

12 Nov 2008 12:19 PM

I realize I can loop thru the list, but I was hoping the built in "find" ability of the IndexOf function would be more efficient.  I have a routine that goes through each item in the list and does a more complex compare on each item and attempts to find the closest match, but I figured if there was an exact match that could be located by the IndexOf function, I wouldn't even need to enter the loop.  For the sake of my education, could you explain how this isn't the intended usage, and maybe as well what is?  Thanks!

MBERRY

12 Nov 2008 12:38 PM
Tom,

The best way I can explain it is like I did in my orig post. IndexOf is doing an object comparison of each item in the list against the item you ask for. When I say "Object" picture in your head something you can click on in the part...a parameter, feature, etc.
So, in your code you are essentially asking if "40mm" exists in the list. "40mm" is not an object in your part, so it is never found. Does that help?

Mike
You are not authorized to post a reply.
Forums > COE Forums > CATIA V5 Programming > List.IndexOf(,) Syntax



ActiveForums 3.6

    

401 North Michigan Avenue, Chicago, IL 60611-4267 | (312) 321-5153 | (800) COE-CALL (U.S.)