Home AMX User Forum AMX General Discussion
Options

length_array & structures

Can one use length_array on a structure. I can't seem to. It would seem somewhat difficult if not impossible inside the structure but what about the upper most level?

sStruct[x].sValue[y].sData[z]

nStructLen = length_array(sStruct) ;

The actual real number of sStruct is 3 and all I get from nStructLen is 0.

If this is impossible what other means is there to make this determination in code other than where this value is defined.

The purpose is to compare structure values written in XML on RAM and reloaded into the structure on start up to compare these values to values that may have been changed or quantities (number of srtuctures) increased.

If quanties and values are the same load the XML and be done but if quantities are different load the XML compare files and if differnet load the new values.

I created a persistent variable to pass to my module to hold this size value from initial start up but was wondering if I could eliminate the var and just use the length_array to determine size changes.

Comments

  • Options
    Joe HebertJoe Hebert Posts: 2,159
    vining wrote:
    Can one use length_array on a structure. I can't seem to. It would seem somewhat difficult if not impossible inside the structure

    Not impossible at all ? if I understand the question correctly.
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    
    DEFINE_CONSTANT
    
    INTEGER nMaxMyStructs = 5
    
    DEFINE_TYPE
    
    STRUCTURE _sMyStruct {
    
       CHAR		Junk[16] 
    }
    
    DEFINE_VARIABLE
    
    _sMyStruct sMyStucts[nMaxMyStructs]
    
    DEFINE_START
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1] {
    
       PUSH: {
          
          //we haven't populated Junk yet so length is 0
          SEND_STRING 0, "'LENGTH_ARRAY(sMyStucts[1].Junk) = ',ITOA(LENGTH_ARRAY(sMyStucts[1].Junk))"
       }
    }
    
    BUTTON_EVENT[dvTP,2] {
    
       PUSH: {
       
          //we put 'Some Stuff' in Junk so length is 10
          sMyStucts[1].Junk = 'Some Stuff'
          SEND_STRING 0, "'LENGTH_ARRAY(sMyStucts[1].Junk) = ',ITOA(LENGTH_ARRAY(sMyStucts[1].Junk))"
     
       }
    }
    
    BUTTON_EVENT[dvTP,3] {
    
       PUSH: {
       
          //shows we can manually set the length of Junk
          SET_LENGTH_ARRAY(sMyStucts[1].Junk,6)
          SEND_STRING 0, "'LENGTH_ARRAY(sMyStucts[1].Junk) = ',ITOA(LENGTH_ARRAY(sMyStucts[1].Junk))"
          SEND_STRING 0, "'sMyStucts[1].Junk = ',sMyStucts[1].Junk"
     
       }
    }
    

    Output when button 1 is pushed first:
    Line 1 :: LENGTH_ARRAY(sMyStucts[1].Junk) = 0 - 19:05:35

    Output when button 2 is pushed second:
    Line 2 :: LENGTH_ARRAY(sMyStucts[1].Junk) = 10 - 19:05:38

    Output when button 3 is pushed third:
    Line 3 :: LENGTH_ARRAY(sMyStucts[1].Junk) = 6 - 19:05:40
    Line 4 :: sMyStucts[1].Junk = Some S - 19:05:40
  • Options
    viningvining Posts: 4,368
    Joe wrote:
    Not impossible at all ? if I understand the question correctly.

    I'm essentially trying to get the value of "nMaxMyStructs" from LENGTH_ARRAY(sMyStucts) of your example. In other words if we think of the structure as a multi dimensional array the number of tables in sMyStructs not the amount of data in an element of the a table or rows in a table or column in a row, etc.

    I just want the numbers of tables. The skeleton or framework of the structure is created at compile time and I'm not looking for data contained in the structure, I'm trying to determine the framework. If that makes any sense! Basically I want to know how many tables of the structure I have to fill or in this particular case how many tables exist so I know how many I should try to compare data from. Comparing possible changes of structure size to that of the structure being loaded from RAM on start up and if there's a difference load the new structure with defaults and forget the old data from file. Possibly populate the existing data into the new structure and load defaults into areas the RAM file can't populate because no previous data existed.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    I must have had too much turkey yesterday because I?m not quite following your logic but I think I understand what you are asking. The only way to know how many tables you have is to count them as you add them in ? or look inside them and see if any data is present. Your max amount of tables is set at compile time so you already know that value. After that it?s up to you to keep inventory to track how many you are using.
    vining wrote:
    I'm essentially trying to get the value of "nMaxMyStructs" from LENGTH_ARRAY(sMyStucts) of your example.
    No can do. But like I said that value is set at compile time so you already know it.

    I have no idea if I?m answering your question but that?s my story and I?m sticking to it.
  • Options
    viningvining Posts: 4,368
    Joe wrote:
    No can do. But like I said that value is set at compile time so you already know it.

    Let me try to clarify.

    Yes, but I'm trying to compare that known qty of tables to the amount of tables contained in an XML file written to RAM.

    Actual my thinking is flawed, cuz if it were possible I would be getting the current number of tables that was just compiled not the amount of tables written to XML unless I parse the XML to make this determination or I would have to do as you suggest and look for data missing in the tables and if for some reason the new compiled structure is smaller than the XML written to RAM when I do my XML_TO_VAR I'll get the error "end of file reached" or something.

    I'm just trying to determine if the stored XML file is still valid when a new program is uploaded any time we go through a DEFINE_START routine such as during a reboot or new program upload. Based on that determination I'll either keep the data in the XML or reload defaults or possibly both, load the structure from the XML and if tables were added just load the new tables with defaults.

    I understand what I need to do now, I think!
  • Options
    mkipemkipe Posts: 10
    LENGTH_ARRAY on array of Structs

    I read the above and it seemed like there were two different thought processes. I'm trying to do what I believe vining was asking and that is to test the length of an array of structures.

    The example by Joe seems to test the length of a member of the struct but not the entire struct. In the help file it says:

    LONG LENGTH_ARRAY (<type> Array[ ])

    <type> may be any intrinsic or user-defined data type.

    but I can't get anything to compile using this syntax. I've tried:

    cmdCount = LENGTH_ARRAY(<Fireball_MioCmdPairs> MioCmdXRef);
    and
    cmdCount = LENGTH_ARRAY(Fireball_MioCmdPairs MioCmdXRef);
    which don't compile due to a syntax error

    and finally

    cmdCount = LENGTH_ARRAY( MioCmdXRef) which seems to keep returning zero as described by vining.

    I can work around by keeping track of the array, but I have a tendency to use built in functions when they are there.
Sign In or Register to comment.