search name for numbers --> function

H

Holgerson

Hi everybody,

here is an issue I just can't cope with: What I try to do is to
analyze a name (e.g. of an array or so) in order to search for numbers
within that name that give me an idea about that very array. It's
easiest explained with an example:

construct array: Mat_IO_DP array_temp_20; (I'm using
numerical recipes)

--> What I need now is to extract that "20" as a double number. If I
do that in the main code it's really no problem whatsoever:

#define STRING(s) #s
........................................

string matrix_string = STRING(array_temp_20);

string numbers ="0123456789";

int pos1 = matrix_string.find_last_of(numbers);

double last = double((int(matrix_string[pos1]) -
48)); //gives me the "0" from the "20" I look for

int pos2 = matrix_string.find_last_of(numbers, pos1 - 1);

double penultimate = double((int(matrix_string[pos2]) -
48)); //gives me the "2" from the "20" I look for

and so forth

Now what is my problem? ->

Naturally I try to perform that whole procedure using a function like

double temp = get_temp_from_name(array_temp_20);

Now the function prototype itself would look like

double get_temp_from_name(Mat_IO_DP matrix_name)

--> It turns out that the code in the function then really deals with
"matrix_name" rather than my argument "array_temp_20" that I actually
try to deliver. Now there is no number in "matrix_name", the thing
goes all wrong. Have you guys ever come across this? Thanks a lot for
any help,



Holger
 
T

tragomaskhalos

Hi everybody,

here is an issue I just can't cope with: What I try to do is to
analyze a name (e.g. of an array or so) in order to search for numbers
within that name that give me an idea about that very array. It's
easiest explained with an example:
[snip]
Holger

Sorry, are you asking if you can extract a *number*
from the *name of a variable*? e.g.

int var10;
int num = extract(var10); // num == 10
std::string othervar23;
num = extract(othervar23); // num == 23
??!!

If so then no of course you can't, you're
labouring under a very fundamental
misconception about how the language (dare
I say most any programming language) works.
The name is just a token; it may get squirreled
away somewhere in the object code as debug
information, but that's it.
 
J

Jim Langston

Holgerson said:
Hi everybody,

here is an issue I just can't cope with: What I try to do is to
analyze a name (e.g. of an array or so) in order to search for numbers
within that name that give me an idea about that very array. It's
easiest explained with an example:

construct array: Mat_IO_DP array_temp_20; (I'm using
numerical recipes)

--> What I need now is to extract that "20" as a double number. If I
do that in the main code it's really no problem whatsoever:

#define STRING(s) #s
........................................

string matrix_string = STRING(array_temp_20);

string numbers ="0123456789";

int pos1 = matrix_string.find_last_of(numbers);

double last = double((int(matrix_string[pos1]) -
48)); //gives me the "0" from the "20" I look for

int pos2 = matrix_string.find_last_of(numbers, pos1 - 1);

double penultimate = double((int(matrix_string[pos2]) -
48)); //gives me the "2" from the "20" I look for

and so forth

Now what is my problem? ->

Naturally I try to perform that whole procedure using a function like

double temp = get_temp_from_name(array_temp_20);

Now the function prototype itself would look like

double get_temp_from_name(Mat_IO_DP matrix_name)

--> It turns out that the code in the function then really deals with
"matrix_name" rather than my argument "array_temp_20" that I actually
try to deliver. Now there is no number in "matrix_name", the thing
goes all wrong. Have you guys ever come across this? Thanks a lot for
any help,

Your problem is not being described rather well so I"m not quite positive
what you are trying to pass.

You could just pass the string itself:

double get_temp_from_name( const std::string name )

I'm not sure what type array_temp_20 is. From your sample code it seems
like you could:

double temp = get_temp_from_name( STRING(array_temp_20) );
although STRING is not a C++ keyword or in the STL (afaik) so am not sure
what it's doing. If array_temp_20 is a char array or char pointer, you can
simply pass the char array and let it be converted automatically.

double temp = get_temp_from_name( array_temp_20 );

Without knowing what array_temp_20 is, what STRING is, etc.. I really can't
help.
 
H

Holgerson

Holgerson said:
Hi everybody,
here is an issue I just can't cope with: What I try to do is to
analyze a name (e.g. of an array or so) in order to search for numbers
within that name that give me an idea about that very array. It's
easiest explained with an example:
            construct array: Mat_IO_DP array_temp_20;   (I'm using
numerical recipes)
--> What I need now is to extract that "20" as a double number. If I
do that in the main code it's really no problem whatsoever:
            #define STRING(s) #s
            ........................................
            string matrix_string = STRING(array_temp_20);
            string numbers ="0123456789";
            int pos1 = matrix_string.find_last_of(numbers);
            double last = double((int(matrix_string[pos1]) -
48));      //gives me the "0" from the "20" I look for
            int pos2 = matrix_string.find_last_of(numbers, pos1 - 1);
            double penultimate = double((int(matrix_string[pos2]) -
48));  //gives me the "2" from the "20" I look for
and so forth
Now what is my problem? ->
Naturally I try to perform that whole procedure using a function like
             double temp = get_temp_from_name(array_temp_20);
Now the function prototype itself would look like
             double get_temp_from_name(Mat_IO_DP matrix_name)
--> It turns out that the code in the function then really deals with
"matrix_name" rather than my argument "array_temp_20" that I actually
try to deliver. Now there is no number in "matrix_name", the thing
goes all wrong. Have you guys ever come across this? Thanks a lot for
any help,

Your problem is not being described rather well so I"m not quite positive
what you are trying to pass.

You could just pass the string itself:

double get_temp_from_name( const std::string name )

I'm not sure what type array_temp_20 is.  From your sample code it seems
like you could:

double temp = get_temp_from_name( STRING(array_temp_20) );
although STRING is not a C++ keyword or in the STL (afaik) so am not sure
what it's doing.  If array_temp_20 is a char array or char pointer, you can
simply pass the char array and let it be converted automatically.

double temp = get_temp_from_name( array_temp_20 );

Without knowing what array_temp_20 is, what STRING is, etc.. I really can't
help.

--
Jim Langston
(e-mail address removed)- Hide quoted text -

- Show quoted text -

Dear Jim,

I think I didn't quite transfer my message. Let me try again: In this
case array_temp_20 is an 100x6-array and STRING()..... is supposed to
be the name of the array as a string (#define STRING(s) #s --> string
array_string = STRING(array_temp_20); or actually, as it is to be
performed within a function --> string array_string =
STRING(array_name);). I don't think this matters though as I try to
extract a number from that name of the variable. So the call in the
main code should look like

double temp = get_temp_from_name(array_temp_20);

and the prototype would look like

double get_temp_from_name(Mat_IO_DP array_name) (Mat_IO_DP here is
the 100x6-array using numerical recipes).

I'd be satisfied if after that the variable temp would be 20.

Maybe your idea of passing the string itself into the function might
work, I'll try this. Please let me know if you know a solution now
that you know better about the issue. Thanks again,

Holger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top