Use string and integer in moon

Asked

Viewed 237 times

1

Well I have a table as follows:

{
        Number: 15007       Name: "Variance"        Value:  35

}

Before I used a txt format that was like this:

15007,Variance,35

The C-code looked something like this:

void read_names(void) {
    struct st *table;
    int var = 0;
    const char *str = NULL;

    table->number = var;
    table->name = str;
    table->value = var;

    return;
}

I want to switch to moon now, I could use libconfig, more I’ve used once and the same is very limited, an example in lib if I were to do the above table:

void read_names(void) {
        struct st *table;
        int var = 0;
        const char *str = NULL;
        config_setting_t *names = .........

        table->number = config_setting_lookup_int(names, "Number", &var);
        table->name = config_setting_lookup_string(names, "Name", &str);
        table->value = config_setting_lookup_int(names, "Value", &var);

        return;
}

So above I could read the table I posted at the beginning of the topic.

But I want to use moon, what I want to know how I use the same way of lib and allow me to use integers and string in the names Number, Name and Value, for example:

table->number = função_lua(L, "Number", &var);

If writing cannot be that way, take into account that it was just an example.

  • I can’t understand what you’re doing and what you’re trying to do. You need to [Dit] the question and put more information that is relevant to the problem.

  • Edited, what I want to know is how I can call the moon for numbers in function table->number = in case that function would have to read the Value of the table.

  • You want to know what the function is to use a value that is numerical instead of a string as was done with Name?

  • yes, only that you would have to read Value: it would be like a C reading that you do printf("Value %d\n", table->name);

  • What did you put in is doing what you want? Because you shouldn’t. What you’re describing is different from what the code is doing. Maybe put yourself in details what you’re doing, where you’re coming from and a complete code is easier to understand.

  • I added new information.

Show 1 more comment

1 answer

1


From what I understand you want the table indexes to be of type number and string "string":

You can handle table indices normally just use the tab ,

Test the code below on the network: https://www.lua.org/cgi-bin/demo

tab = { 'indice1', cad = 'cadeia', 'indice2'}
print(tab[1],tab.cad,tab[2])
-- indice1  cadeia  indice2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.