DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Inconsistent indexing of for each channel in channels from 2012 to 2021

Solved!
Go to solution

I am converting a script over from 2012 to 2021. It effectively goes through each channel and if a different length is found from a set value it will remove that channel. In 2012 using a for each channel in channels loop it will correctly go through each channel and remove the typically 3 channels in a row that aren't at the same length as the others. However when this same code is used in 2021, it will remove the first, skip the second then remove the third. As shown below in a similar demo I am removing every other channel, there is an inconsistent number of channels that are looped through as there is a skip when channels are removed from the group.


Code Used for demo

dim oChn, oChns, count
set oChns = Data.Root.ChannelGroups(1).Channels
count = 0
LogFileWrite("----------------------------------------------------")
for each oChn in oChns
if count mod 2 = 0 then
LogFileWrite("Remove: " & oChn.Name)
LogFileWrite("Count: " & count)
LogFileWrite("----------------------------------------------------")
oChns.Remove(oChn.Name)
else
LogFileWrite("Non-Removed: " &oChn.Name)
LogFileWrite("Count: " & count)
LogFileWrite("----------------------------------------------------")
end if
count = count +1
next

 


Starting channel list

BrandonHosken_0-1644955877667.png


2012 Output

----------------------------------------------------
Remove: 10VEHC000063ACXP
Count: 0
----------------------------------------------------
Non-Removed: 10VEHC000064ACXP
Count: 1
----------------------------------------------------
Remove: 10VEHC000065ACXP
Count: 2
----------------------------------------------------
Non-Removed: 10VEHC000066ACXP
Count: 3
----------------------------------------------------
Remove: 10VEHC000067ACYP
Count: 4
----------------------------------------------------
Non-Removed: 10VEHC000068ACYP
Count: 5
----------------------------------------------------
Remove: 10VEHC000069ACYP
Count: 6
----------------------------------------------------
Non-Removed: 10VEHC000070ACYP
Count: 7
----------------------------------------------------
Remove: 10VEHC000071ACYP
Count: 8
----------------------------------------------------
Non-Removed: 10VEHC000072ACYP
Count: 9
----------------------------------------------------

 


2021 Output

----------------------------------------------------
Remove: 10VEHC000063ACXP
Count: 0
----------------------------------------------------
Non-Removed: 10VEHC000064ACXP
Count: 1
----------------------------------------------------
Remove: 10VEHC000065ACXP
Count: 2
----------------------------------------------------
Non-Removed: 10VEHC000067ACYP
Count: 3
----------------------------------------------------
Remove: 10VEHC000068ACYP
Count: 4
----------------------------------------------------
Non-Removed: 10VEHC000070ACYP
Count: 5
----------------------------------------------------
Remove: 10VEHC000071ACYP
Count: 6
----------------------------------------------------

 


Is there a different command besides remove that retains the ordering of the channels found in oChns? Or is this a bug in diadem? Any help would be sincerely appreciated

0 Kudos
Message 1 of 3
(1,334 Views)
Solution
Accepted by topic author BrandonHosken

Hi BrandsonHosken,

 

I can explain the differences. You are working with a channels collection (Data.Root.ChannelGroups(1).Channels represented by the variable oChns). This collection must reflect the current situation of the Dataportal at any time. If you change something in the Dataportal the collection must be replicate this change – this is also true for its variable.

 

This was a bug in DIAdem 2012 and fixed in later versions.

 

In your case it is necessary to collect the channels to be removed in a first step and delete them in a second step. Below you find an example:

 

 

dim oChn, oChns, count, oChnListToRemove

set oChns            = Data.Root.ChannelGroups(1).Channels
set oChnListToRemove = Data.CreateElementList

count = 0

LogFileWrite("----------------------------------------------------")

for each oChn in oChns
  if count mod 2 = 0 then
    LogFileWrite("Remove: " & oChn.Name)
    LogFileWrite("Count: " & count)
    LogFileWrite("----------------------------------------------------")
    call oChnListToRemove.Add(oChn)
    'oChns.Remove(oChn.Name)
  else
    LogFileWrite("Non-Removed: " &oChn.Name)
    LogFileWrite("Count: " & count)
    LogFileWrite("----------------------------------------------------")
  end if
  count = count +1
next

for each oChn in oChnListToRemove
  call oChns.Remove(oChn.Name)
next

 

 

Greetings

Walter

Message 2 of 3
(1,292 Views)

Thank you I appreciate the help and the explination!

0 Kudos
Message 3 of 3
(1,272 Views)