473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic variable name. plz help

cpp
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.
thx alot
Jul 22 '05 #1
10 3452
cpp wrote:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.
thx alot


Why not use std::map<std::string, double>?

#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<std::string, double> mymap;
std::string s("whatever");

mymap[std::string("whatever")] = 4;
std::cout << mymap[s] << std::endl;

return 0;
}

Jul 22 '05 #2


cpp wrote:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same identifier
a macro might help passing only one identifier to but creating a string
with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.

Jul 22 '05 #3
cpp wrote:

I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.

Good news! You can stop worrying. You can't do it in general. There may
be platform specific ways to do symbol lookup (assuming things were even
compiled to have symbols) but that's highly unportable.

Why don't you tell us what you are actually trying do, rather than
describing your failed solution to the problem?

Brian Rodenborn
Jul 22 '05 #4

"cpp" <me@on.the.web> wrote in message
news:R0*******************@twister.nyroc.rr.com...
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's

value.

I think your trying to do something that would be very natural in a
scriptoing language like perl or javascript . In C++ there are
typically adequate (and usually more efficient) ways to do the same
thing, once you learn the idioms.

What problem are you trying to solve?

Jonathan
Jul 22 '05 #5

"Jonathan Turkanis" <te******@kangaroologic.com> wrote:
I think your trying to do something that would be very natural in a


Ugh -- should be 'you're'!

Jonathan
Jul 22 '05 #6
cpp
Patrik Stellmann <st*******@tu-harburg.de> wrote in
news:c0*************@uni-berlin.de:


cpp wrote:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's
value.

I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same
identifier a macro might help passing only one identifier to but
creating a string with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.


This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;
The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.
Jul 22 '05 #7
cpp wrote:
Patrik Stellmann <st*******@tu-harburg.de> wrote in
news:c0*************@uni-berlin.de:


cpp wrote:

I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's
value.


I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same
identifier a macro might help passing only one identifier to but
creating a string with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.

This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;
The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.


Use a std::map<std::string, double>. See my earlier post.
Jul 22 '05 #8
"cpp" <me@on.the.web> wrote in message
news:E_*******************@twister.nyroc.rr.com...
Patrik Stellmann <st*******@tu-harburg.de> wrote in
news:c0*************@uni-berlin.de:


cpp wrote:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's
value.

I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same
identifier a macro might help passing only one identifier to but
creating a string with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.


This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;
The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Why must this be done!? In your compiled executable, you do not have any
variable names anymore anyway. It's all just addresses and numbers.
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #9
> This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;
The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.


That's simply impossible in c++. But let's imagine it would be possible
and you have now such variables in your code. What would it be good for?
At runtime - when you have loaded that file - you can hardly modify cour
code and add something to use those variables while at compiletime you
could add such code but don't know the names of those variables!
Maybe you should tell us little more *why* you think you need that
because most likely there's your mistake. I'd guess the use of std::map
- as already suggested by others - will be the right way in your case...

Jul 22 '05 #10
cpp
cpp <me@on.the.web> wrote in news:E_bZb.30277$um1.23571
@twister.nyroc.rr.com:

I used <map>, it works perfectly. I didn't know this was a better way to go
because, quite simply, I'm a novice.

Thanks a ton, you guys have been extremely helpful :)

now to get back to my NewtonsMethod prog.
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken Halley | last post by:
How does one refer to a variable name using a variable within a variable name??? For example, I want to perform a SQL query and use the results of the query to determine which variable to assign a...
7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
by: devx777 | last post by:
Hello, I am trying to find some information or an example on how to build a dynamic query in DB2 that would allow me to join a table which its name is stored as a field value on another table....
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
2
by: viki1967 | last post by:
Hi there. I have a problem in to recover the values of a dynamic form. The form is this: strSQL = "SELECT * " strSQL = strSQL & " FROM " strSQL = strSQL & " TBL "
7
by: bprocopio | last post by:
Please help. I'm stumped. I need to create a dynamic variable in a procedure that will be used to update a variable of the same name in a table. i.e. the name in tblAnalysisScores are...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.