Not sure what it's called to ask for it, but I have an array of global variable names, list[]:
int mhum=69;
int mtem=32;
int msmk=9;
int alm1=21;
int alm2=22;
void setalm() {
String list[]={"alm1","alm2","mtem","mhum","msmk"};
for (int ii=0;ii<5;ii++){
tmp=readFile(LittleFS,("/"+list[ii]).c_str());
// list[ii] = tmp; // This doesn't do it, this sets list and not alm1, etc..
// This would do some of what I want..
if (list[ii]=="alm1") { alm1=tmp; }
if (list[ii]=="alm2") { alm2=tmp; }
//etc, but this would be longer, all hard coded and I could only set known variables I have preset.
}
}
Basically if I made the function accept any variable to assign it'd be more useful. Is there any way to get list[1] to act like alm1 and list[2] to be alm2, etc. so I can assign the value directly to what ever variable comes up?
This seems to want to use the variable name and not the string. So this doesn't work either: int *tmp2 = &list[ii];
Any ideas on what I am looking for?
EDITED:
This is a very bad idea to change essentially any variable from a web server. You'd probably spend just a much code trying to prevent misuse.. as the list of variables to change. So prelisting them in if statements, the way vanderdecken proposed or even just using object/json arrays might be better solutions to this.
That said, if anyone does know a way to do it, I'd still be interested in testing it to see how it can be abused and share the results. But no, do not recommend putting that into anything you want to actually use or sell!
Variable names are "optimized away" by the compiler. The program doesn't need to know the actual names used; it deals with memory addresses. (When a program is compiled for debugging, there is extra "debug info" that has a mapping between the names and addresses, so you can see those variable names in a debugger.)
In interpreted languages like PHP, those names are available. Languages like Java and JavaScript -- in a case where they actually behave as similarly as their names suggest -- obfuscate variable names, converting meaningful names to arbitrarily short ones: a, b, c, etc.
Your snippet has the comment
Variables in C++ are also "preset" -- it's unclear how you would use a variable in your sketch otherwise. Suppose for example, you JSON for configuration, so it is all in the same file
{
"mhum": 69,
"alm2": 32
}
If you read that into JsonDocument config, then you could have code like
doSomething(config["mhum"], config["alm2"], 47);
Is it slower to read and write a JSON object? Yes. Can you tell? Maybe not. Even if you had code like
int mhum = config["mhum"];
int alm2 = config["alm2"];
you're never getting away from typing those names.
Thanks much one piece fan, pretty much what I was seeing from the other page. I can't go by just the string that way. Trust me, you don't want the whole code. 2000+ lines of arduino, html and js... scary.
Basically I get a string from the webserver, "?alm1=on", for setting alarms and other settings. I can take the string "alm1" as the filename and save "on" to file, easy. Now, without a bunch of repetitive if statements like,
if (list[ii]=="alm1") { alm1=tmp; }
how would I also set/change the variable, alm1. Say if I took the function to this instead:
String alm1,alm2;
setalm("alm1","off");
setalm("alm2","on");
//whatever other variables
void setalm(String list,String dat) {
[[list]]=dat; // this doesn't work, I did something like this in another language...
}
[[list]] basically becomes the variable alm1 or alm2 based on the string set, like how the PHP code works:
I also figured that it could be easier in interpreted languages, hence finding the examples, but either way it's still really just pointing to a memory address at the end of the day.
I figure I just have to make a bunch of if statements then to cover all the multitudes of options I have. fun.
There was also the security concerns of changing any variable from web server access that I'd have to address if I went that route I mentioned... Not as big a deal for home networks vs public.. but still.
Even if 99% of it is irrelevant? Bit wasteful. Knowing how I set up my smoke sensor has nothing to do with my data question. However, yes I can see how in some cases the extra data could help. I've seen questions with one line of code and they basically ask how to make a guided missile.
If anyone finds this and has the same question, I went with the if statements I mentioned earlier. Looks repetitive and ugly but works good enough. If anyone has a better solution, I'm open to it.
It seems you didn't understand the answer by @van_der_decken
If you organize the variables in array, it makes it assignment much easy without a bunch of repetitive "if"
Reread the code in #3
Been programming in general for over 30 years. I can guarantee none of the rest of my code (which works fine) requires variable assignment. It's something I have done before, in several other languages, just not here. Hence asking if anyone has done it here with arduino/c++. I posted more than enough to explain what I am talking about. Thanks for your time.
I read and reread the reply. I even reread my reply.
If all I get from the server is "alm1" (or any other variable name), I still need to add the &alm1 (or other name) to the paired list he has there, referring back to the original question of converting "alm1" into said variable to begin with. In the original post, I just had the list of variable names.
&alm1 is a pointer to the address of alm1, so what he has there will possibly work, for what he has there. However, if my server spits back "alm5", that's not on the list, it won't work. My way or his, I have a list of pairs or a list of if statements, it's not dynamic like the php example:
$a = "alm1";
$alm1 = "off";
$$a = "on"; // forces $a to be "alm1" that it's set to.
echo $alm1;
>> on
As already noted above, in C++ after compilation the variable names used by the programmer are replaced with addresses in memory. Therefore, for the code, a reference to yours "alm1" or "alm5" means nothing. What you did in php - in C/C++ simply does not work
You need to learn C and change your approach.
I did change my approach. I used the if statements. I said that, you quoted it. Actually I even mentioned them in the first post. I was looking for an answer as to why I couldn't find how to do the variable thing directly.
Yeah, I shortened the array for the example. I missed the number in the copy/paste of the for loop, I mostly wrote the example from scratch because I couldn't figure out how to implement it the way I wanted to. I was working with arrays in the real code, I didn't need the example to be over complicated. If it can't be done simple, it can't be done more complicated. Using this wouldn't have made it any easier to read:
int alm1[]={2,1,0,0,0,0,0,0,0,0,0,0,0}; //h,m,mon-sun,flag,b1,b2,sms
int alm2[]={2,0,2,1,2020,0,0,0,0}; //h,m,d,m,y,flag,b1,b2,sms
I don't see any advantage to having a separate set of variables and list of names. Why not combine them into a single entity so they never get out of synch?
But what if you don't have a variable named "alm5"? What should happen then? In PHP, does it just create a new variable? But if a variable named "alm5" doesn't exist, your code can't do anything with it anyway, so it might as well not get set to a value.
Since you know what all variables your program has, what's wrong with the solutions given which have either a) an array of structs with a name and pointer to a value, or b) a map of names and values? And if a variable like "alm5" doesn't exist, you won't include it in the array or map.