Iterate List or Array of Strings within a Loop

If I have some conditional logic defined and the code I need to run within an IF block is just repeated commands, is it possible to define a list or an array of string values and then have a set of commands iterate through each value one after the other rather than duplicating code?

I wasn’t sure if there were any gotchas with running a loop like this if possible within an IF block mattered or not but if so, I’m curious if this is possible if even just not within an IF block, if support could provide a simple example of how to write such a loop to iterate two dummy string values across maybe two separate commands?

Technical Specs

  • Flexy 201 running from BASIC IDE

Example Logic

IF(TriggerTag1@ = 1) THEN
    SETSYS TAG, "Load", "Tag1"
    SETSYS TAG, "LogEnabled",1
    SETSYS TAG, "SAVE"
    SETSYS TAG, "Load", "Tag2"
    SETSYS TAG, "LogEnabled",1
    SETSYS TAG, "SAVE"
    SETSYS TAG, "Load", "Tag3"
    SETSYS TAG, "LogEnabled",1
    SETSYS TAG, "SAVE"
    PRINT "Logging Enabled Successfully"
ENDIF

@PJ_IOT

So to confirm, you want to convert your IF there to a for-loop?

You could just do:

IF (TriggerTag1@ = 1) THEN
    FOR i% = 0 TO 3 STEP 1
        tagname$ = "Tag" + STR$ i%
        SETSYS TAG, "LogEnabled", 1
        SETSYS TAG, "SAVE"
    NEXT i%
ENDIF

1 Like

The problem may be the tag name is not the same in sequence as that example though so it could be Sifter and Silo for example. I wasn’t sure if you could define a list or an array like array$ = (Sifter, Silo, Airlock) and then iterate through those string values from that list one after the other (e.g. FOR i% IN array$)

@PJ_IOT

Unfortunately there isn’t a very good implementation for lists in the eWON, though you can use the internal array system to accomplish something very similar.

Basically you would need to create an array and populate it. In my example I simply used a for-loop to populate random data but you can easily do it manually. Also it is very important to declare the array in the init section so it is globally scoped.



Init Section:

DIM tags$(10, 50) REM Declares an array of 10 items with a max length of 50 chars

REM Populate with for loop
FOR i% = 1 TO 10 
    tags$(i%, 1 TO 50) = "Tag" + STR$ i%    REM Tag1, Tag2, etc..
NEXT i%

REM Populate Manually
tags$(1, 1 TO 50) = "TankLevel"
tags$(2, 1 TO 50) = "Valve"




Next we simply call a listener function and loop our array.

Listener Section:

for i% = 1 TO 10
    SETSYS TAG, "Load", tags$(i%, 1 TO 50)
    SETSYS TAG, "LogEnabled",1
    SETSYS TAG, "SAVE"
next i%
1 Like

That’s interesting… I’m going to play around with this some.

@PJ_IOT

Sounds good!

If you run into any hiccups, let me know.

A post was split to a new topic: LogEnabled