Sign in to follow this  
Followers 0
evanmars

Getting Index of particular array element

4 posts in this topic

Say I have an array of strings and a string I want to find in said array.  Is there a way to return the index of the string that matches the string I am searching for?

Array = [DOG],[CAT],[MOUSE],[HORSE],[WOMBAT]

Search string = MOUSE

Find "MOUSE" in Array = 2

Share this post


Link to post
Share on other sites

OK, I guess FIND is my instruction.

Now, how do I get it to search past the first element of my array.

If I try to find a string that matches the first element, I get a result of 1, but if I try a string anywhere else in the array, it returns 0.

FIND 'ABC' in 'ABC','DEF','GHI','JKL' returns 1

FIND 'DEF' returns 0

 

Edited by evanmars

Share this post


Link to post
Share on other sites

What I ended up doing was iterating through the array and when I found a match, I used the iteration number instead of trying to get the element number. 

Tomaytoes/Tomahtoes

Share this post


Link to post
Share on other sites

We did a system where we had to track parts based on their barcode, then go back and find them.  I did it in SCL, here is what it looks like.  Just a simple FC that i called and passed the barcode we read from the part.  the FC returns the Index value of where in the array it found the match plus a couple of other things not relevent to your question.

 

Good luck.

 

FUNCTION FC9 : VOID

VAR_INPUT
    SerialNum : STRING[50];
END_VAR

VAR_TEMP
    Index : INT;
    TempString : STRING[16];
END_VAR

VAR_OUTPUT
    Location : INT;
    Status : INT;
    TestPlan : INT;
END_VAR

    TempString := SerialNum;
    Index := 1;
    FOR Index:= 1 TO 499 BY 1 DO
        
        IF PartTracking.Database[Index].TrackingNumber = TempString THEN
            Location := Index;
            Status := PartTracking.Database[Index].PartStatus;
            TestPlan := PartTracking.Database[Index].TestPlan;

            EXIT;
    
        ELSE
            Location := 0;
            Status := 0;
            TestPlan := 0;
            
        END_IF;

    END_FOR;

    ;
END_FUNCTION

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0