Unitialize a variable

  • This seems like a weird question, but is there a way to UN-initialize a variable?

    I'm trying to create stack handling subs (push/pop) but I can't tell how tall the stack is unless I somehow keep a separate index on which elements are actually empty. I was thinking I could use ON_ERROR_PROCEED to determine the stack height of an array, but this only work until I pop the top item, and I don't have a way to tell that item is popped any more. I could just write a known value and know that's empty, but that way I can never use the value intentionally.

    I could always have element 1 be the stack height, but that would only really work for a REAL or an INT and feels sloppy.


  • I do not understand quite but...

    If you need to keep track of count of SOMETHING, why first element should be the count? Why not use separate element (not part of stack or array) and make sure it is type you need (even if it is different feom type of data on so called stack)?

    Did you consider using structures? Then your array or stack coukd be a collection of variables that EACH element has own flag “initialized”

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • I'd absolutely just have a separate INT to keep the stack height, but I'm trying to make a library I can use at will on any array, so I'd have to feed this function a separate variable to track the stack height.

  • There's not really any way in KRL (that I'm aware of) to change a variable's state from #INITIALIZED to #DECLARED (as VARSTATE() would return it).

    Usually, I handle this one of two ways:

    1. Setting the array element to some ridiculous value that can be expected to never occur in normal operation (like -9999.999) and checking for that specific value on the fly

    2. Use an Array of STRUCs, and creating my STRUC to contain both a value, and a valid/invalid flag. I often create an ENUM to use as the "status" variable, which gives me some more flexibility. Something like:

    ENUM MyArrayMemberStateEnum Defined, Undefined
    STRUC MyArrayStructure REAL MyNumValue, MyArrayMemberStateEnum MyArrayMemberState
    DECL MyArrayStructure MyArray[100]
    MyArray[1] = {MyNumValue -9999.999, MyArrayMemberState #Undefined}
    MyArray[2] = {MyNumValue 227.321, MyArrayMemberState #Defined}
    ; ... and so on

    Either method requires that any program that "pushes" or "pops" to the array take care of altering the value of the array member(s) correctly.

    If I can't count on my array to be perfectly contiguous, sometimes I'll FOR-NEXT through it and copy the "valid" values into a continuous, temporary array (which also allows me to count up the number of valid entries as I go). OTOH, if I can count on my array to be contiguous, I may add a #Final to my ENUM, and move forward that way.

    Getting really fancy, I've sometimes created STRUCs that contained multiple variables of multiple types, and an ENUM that flagged which one (the Bool, the Int, the Real, etc) was "valid" for that array entry. This can get a bit convoluted, but given KRL's limitations, sometimes that's necessary if you're building a general-use library for dealing with arrays.

    Edited once, last by SkyeFire ().

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