Cancel
Showing results for 
Search instead for 
Did you mean: 

Find method doesnt seem to function properly

Creator
Creator

Example:

Im working on a table (called values) that looks like this:

 

1         1         3         2        2     <-- some sort of index numbers

12,1    11,9    9,8      9,3     5,3  <- generic values

 

What I want to do is the following, I have a table that indicates the order of the index numbers, so: 1,1,2,2,3, lets call this 'sequence'.

 

For i:=1 to sequence.xdim

Values.setcursor(1,1)
Find the position of sequence[i] in the index row 1
Copy that column to another table

Cut that column out

next

 

The goal here is to 'sort' a big table of data according to the index numbers.
Here is what happens:

It finds the first index value (1), copies the column to the other table, and cuts the column (all good).

But then, it fails to find the second index value of 1 (second column in the original table). It skips this, goes to the next number in the sequence (2) and succesfully processes the first column, but again fails to find the second column.

 

I relocate the cursors to (1,1) after each copy/cut and tell the find method to look in the first row of values only.

 

What could be going wrong here?

 

Kind regards,

T

 

 

 

 

7 REPLIES 7

Re: Find method doesnt seem to function properly

Siemens Phenom Siemens Phenom
Siemens Phenom

I did not check what actually happens, but I think I know the reason for the behavior:

 

You can call 'find' multiple times. Each time 'find' is called again, it continues the search, i.e. it starts looking for the value in the next column. When you delete the current column, the content of the next column will be moved into the current column. So that column will be skipped when you call 'find' again.

 

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Find method doesnt seem to function properly

Siemens Phenom Siemens Phenom
Siemens Phenom

If I understand this correctly, you immediately cut a found entry.

With this, you implicitly change the indices of all subsequent entries:

So, after the first cut, the table, previously:

    1         1         3         2        2     <-- some sort of index numbers

    12,1    11,9    9,8      9,3     5,3  <- generic values

looks like this:

    1         3         2        2     <-- some sort of index numbers

    11,9    9,8      9,3     5,3  <- generic values

 

Now your loop counter increases to 2 so you continue searching in the now second column which is the once that was the third column before.

 

To fix this, you can either count manually and skip increasing the counter if you found something or you count backwards:

 

for sequence.xdim downto 1   etc...

Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution

Re: Find method doesnt seem to function properly

Creator
Creator

@MichaelJoos
Isnt this prevented by setting the cursor to position (1,1) after each modification of the table?

@PeterKomarek
I only use a loop to walk throught the index numbers that are specified in the 'sequence' table. So lets say sequence contains 34, 65, 88.

It first searches for 34, then it searches 65 etc. I do not loop through the columns of the 'value' table that I am searching in.

Re: Find method doesnt seem to function properly

Siemens Phenom Siemens Phenom
Siemens Phenom

It might help if you posted the full code of your for-loop. Maybe the problem lies in the indicated code parts.

 

By the way, you can start debugging your code if you press F11 in the method editor. In many cases going through teh code step-by-step relatively quickly points to the problem at hand.

Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution

Re: Find method doesnt seem to function properly

Creator
Creator

Can it be that find has some troubles with running in debug mode> Either that, or I was not paying attention carefully enough, because it seems to be working fine now.

 

Sorry for wasting your time, and thanks a lot!

Re: Find method doesnt seem to function properly

Siemens Phenom Siemens Phenom
Siemens Phenom

Note that you should never enter 'find' or 'setCursor' into the Expressions tab of the method debugger, because these functions have side effects. Plant Simulation evaluates the expressions after each debugger step, so the side effects would affect the method execution.

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Find method doesnt seem to function properly

Creator
Creator

I indeed had the find function entered into the Expressions tab. Thanks for the feedback, I will keep that in mind!