LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Get value for variable name defined as string (http://www.linuxquestions.org/questions/programming-9/get-value-for-variable-name-defined-as-string-769890/)

webquinty 11-18-2009 04:42 AM

Get value for variable name defined as string
 
Hello,

I would like to known if it possibe to obtain a value of variable defined as string.

For example:

char* varname[] = {"test", "dog"}
test = 5;
dog = 8;

Can I obtain value of varname[1]???

I use C++.

Best regards.

bigearsbilly 11-18-2009 04:55 AM

homework eh?
at least try something.

webquinty 11-18-2009 06:04 AM

Quote:

Originally Posted by bigearsbilly (Post 3761054)
homework eh?
at least try something.

Hello,

I am sorry, but I dont understand what are you tried to say.
Homework??? at least, ten years or more last time when I have opened a book to study.

I have try some code, but it is to insert all variables in a list.

Code:

#include <iostream>
#include <map>
#include <utility> // make_pair
using namespace std;
 
int main()
{
        typedef map<char, int> mapType;
        mapType myMap;
 
        // insert elements using insert function
        myMap.insert(pair<char, int>('a', 1));
        myMap.insert(pair<char, int>('b', 2));
        myMap.insert(pair<char, int>('c', 3));
        myMap.insert(mapType::value_type('d', 4)); // all standard containers provide this typedef
        myMap.insert(make_pair('e', 5));          // can also use the utility function make_pair
 
        // erase the first element using the erase function
        mapType::iterator iter = myMap.begin();
        myMap.erase(iter);
 
        // output the size of the map
        cout << "Size of myMap: " << myMap.size() << '\n';
 
        cout << "Enter a key to search for: ";
        char c;
        cin >> c;
 
        // find will return an iterator to the matching element if it is found
        // or to the end of the map if the key is not found
        iter = myMap.find(c);
        if( iter != myMap.end() )
                cout << "Value is: " << iter->second << '\n';
        else
                cout << "Key is not in myMap" << '\n';
 
        // clear the entries in the map
        myMap.clear();
}

but it is very hard to handle several variables.
Then, I have look for information about it, and I dont find clear information (tcl_getvar of tcl library, but I dont know if I can use it)

And now, I am here, to look for help, but I see that it is very hard to find it.

And know in my language, No se por que te pones asi o por que me saltas con eso de que si son "homework". Me parece que la respuesta esta fuera de contexto y no tengo ganas de discutir con nadie.

Best regards

ghostdog74 11-18-2009 06:20 AM

since its not homework, then tell us what you are trying to do? A low level system programming project?? driver programming?? game programming?? If they are not one of these, then i suggest learning and using a scripting language instead. they have easier to use data structures and much more.

webquinty 11-18-2009 06:30 AM

Quote:

Originally Posted by ghostdog74 (Post 3761133)
since its not homework, then tell us what you are trying to do? A low level system programming project?? driver programming?? game programming?? If they are not one of these, then i suggest learning and using a scripting language instead. they have easier to use data structures and much more.

Ok!!

Well, I have develop program, Qt with Rt threads that controlled a machine.
It works fine.
Now I am interesting to read some variables in a webserver.
My idea is use mysql server and connect webpage to mysql table where is a name of var and value. Webpage and mysql works.

But now I have problem. I have a file where is the name of variables that I would like to read value a insert in mysql table, and I dont know how to eval value for variable name defined as string.

Code:

sprintf(sql_consulta, "UPDATE watch set value = '%ld' WHERE name='contador1'", counter.general);
mysql_query(conn, sql_consulta);

For example, counter.general is a var of program, and in file there is a line with this string "counter.general".

More or less, this is I trying to do.

Best regards.


All times are GMT -5. The time now is 12:34 AM.