Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a character string as a variable name

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
Sorry for the ambiguous, nonsensical thread title.

What I have is a header file that defines a class...this file is the output of a particular program. So it's something like this:

class Box{

int List1 = [ 1 2 3 4 5 6 7 9 29 1];
int List2 = [ 1 2 3 4 5 6 7 9 29 1];
int List3 = [ 1 2 3 4 5 6 7 9 29 1];
...
int Listn = [ 1 2 3 4 5 6 7 9 29 1];
}


And so, I want my program to access each of these member variables...however, I don't want to type out &quot;List&quot; n many times, i.e.: &quot;cout << List1;&quot;, &quot;cout << List2;&quot;...I want to do something like this (pseudocode, obviously):

string list = &quot;List&quot;
for(j=0; j<n; j++)
{
for(i=0; i<10; i++)
{
cout << Box.(List[j]);
}
}


How would I go about doing this? Sorry if this is confusing...
 
[tt]
Try using a double scripted array and a for loop. E.G. lists like:

const int size = 3;

int List1[size] = { 0, 1, 2 };
int List2[size] = { 9, 10, 11 };
int List3[size] = { 3, 4, 5 };


can be grouped into a sigle array like this:


const int size = 3;
const int lists = 3; //number of lists

int listGroup[lists][size] = { { 0, 1, 2 },
{ 9, 10, 11 },
{ 3, 4, 5 } };


Doing so let's you print out the varibles with embedded for loops thusly:

//for each list
for( int k = 0; k < lists; k++ )
{
cout << &quot;List &quot; << k + 1 << &quot;:&quot;;
//for each list entry
for( int j = 0; j < size; j++ )
cout << ' ' << listGroup[k][j];

cout << '\n';
}


Let me know if I can help further.
[/tt]
 
Thanks for the input.

Let me give a little more background...I'm using an exporter for MilkShape3D that takes a 3D model and creates a header file. This header file has a variable for each shape in the model. So if there are five shapes and the model is named &quot;Box&quot;, then the header file will have:

Box01 = {1,2,3}
Box02 = {1,7,3}
Box03 = {2,9,3}
Box04 = {4,6,3}
Box05 = {4,5,6}


So what I want my program to do is open any such header file and iterate through these member variables. Some of the header files may contain hundreds of shapes (each shape is described with hundreds of vertex coordinates)...so that's why I'm trying to find a way to automate it by passing in a string to use as the variable name.

Yeah, I know this exporter probably isn't the best designed...but I don't want to write my own...yet ;).
 
At run time, it is absolutely impossible to refer to a variable by computing its name.

You probably will have to write a lexical analyzer to process this. And believe me, that's no piece of cake.

Though, I would recommend never shipping your distribution with your models in a human-readable file... it makes them too easy to steal... Unless you're using the GNU general public license

[sub]I REALLY hope that helps.[/sub]
Will
 
You could put 'em all in a std::map, like

typedef std::map<CString,List> MyMap;

and add it (roughly) like

map.insert(&quot;Box01&quot;,someList);
map.insert(&quot;Box02&quot;,someOtherList);

Then you could access the lists like:

List myList = map[&quot;Box01&quot;].second;
Or you can iterate trough all lists is that's what you wanna do using map.begin() & map.end() etc.

Sure, it isn't really a variable with a certain name, but anyway...

[sub]Nerdy signatures are as lame as the inconsistent stardates of STTNG.[/sub]
 
Using a map has the same problem that using a 2D array does: finding a way to automate putting the values into it.

I'd do it by making a Perl script to look at the header that MilkShape3D generated, then generating some C++ source to put references to all these variables into a map, vector, matrix, or whatever I needed.


However, there might be a more clever solution here. What is the type of the variables Box01, Box02, etc.?

Does the header look like this?:

Code:
typedef int* Shape;

Shape Box01 = { 1, 2, 3 };
Shape Box02 = { 1, 7, 3 };
If it does, you may be able to get away with changing that typedef to be the name of a class you write.

The constructor of that class could cause it to register a copy of itself into a certain map/vector/whatever.

You can keep the same array initializer syntax if you make an array member public, so it'll have the same effect as assigning a list of values to a struct. Of course, theoretically, it should be
Code:
Shape Box01 = { { 1, 2, 3 } };
, but I think most compilers will allow the other way (maybe with a warning).

Let me know if that made any sense at all.
 
You may be able to read your variables into a &quot;generic&quot; array that is named ahead of time (ie. var[j]), where the first subscript (i) will denote the shape type and the second subscript will match those that you are reading in.

Then you could cleverly have a second array of strings (ie. varName) that match the first subscript and will return the name of the shape.

Hopefully this will help you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top