View unanswered posts | View active topics It is currently Fri Oct 24, 2014 2:25 pm






Reply to topic  [ 7 posts ] 
Variables named by program 
Author Message
Rookie

Joined: Fri Mar 30, 2012 6:01 pm
Posts: 3
Post Variables named by program
I'm doing a project in which I'm trying to take vex components to a new level. I've discovered happily that I can name variables after the task main, as long as they aren't called prior to being named (ideally it would be better if it assumed an unnamed variable as 0 when it was called upon for what i'm trying to do, my compiler for another program does that). But now i'm having difficulty having the program name the variable.

In my other programs, i was able to make variables names such as
int "coorx+varget("fc")"
in which it would create a variable named, for example, "coorx6" assuming the "fc" var was on 6. In this sense, the variables would be created as needed upon further usage of the program. I can't preset these variables, they have to be made during the machine's trial. The issue is, i can't seem to find a way to pull off something like explained above.

It always gives me errors when i try to conjuncture variables inside variables. So i'm asking any experts, how can i do this?
in the code i want something like this
if(this that){
int "coorx(fc)" = ([pot] + [time] - [coory(fc)])
fc = fc +1
}

that's just a silly example where the vars don't relate to my actual code, but this code specifically could be something i can do with what i'm asking for.

So, any ideas?

Thanks.


Fri Mar 30, 2012 6:13 pm
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Variables named by program
I don't think you can declare variable names derived from the results of other variables or other expressions in ROBOTC. (Or even C for all I know, though I could be wrong.)
Also, you can't technically declare any new variables at Run-Time anyway in ROBOTC, as it is all pre-calculated before hand which variable is going to be stored where in the Cortex's memory.

What you might want to do is consider using an Array, or using Structs.

First off, an Array is a bunch of the same type of variable organized together into one large variable, with the individual parts accessed using the large variable name and a reference number: (I.E MyVar[4] = 20. This would set the fifth element (because the elements start at 0) of the MyVar array to 20.)
You can read about them here: (Most of the info on that page applies to ROBOTC*)
http://www.cplusplus.com/doc/tutorial/arrays/

Secondly, a Struct is bunch of many types of variables (potentially), organized together into one bigger variable, with the individual parts accessed using the bigger variable name and the small parts variable name. This is a little more complicated, but looks like this (after you've made your type and declared your variable): MyVar.NumOfClaws = 16; MyVar.NameOfCat = "Sally"
You can read about them here: (Most of the info on that page applies to ROBOTC*)
http://cprogramminglanguage.net/c-structure.aspx


*The info that wouldn't apply is regarding pointers, as ROBOTC doesn't support those.


Sat Mar 31, 2012 9:58 am
Profile
Rookie

Joined: Fri Mar 30, 2012 6:01 pm
Posts: 3
Post Re: Variables named by program
Thanks, that's pretty much what i was looking for. Both of them are very helpful.


Sat Mar 31, 2012 12:00 pm
Profile
Rookie

Joined: Fri Mar 30, 2012 6:01 pm
Posts: 3
Post Re: Variables named by program
I've found what makes me able to do the naming of variables like how i want, maybe someone could assess this and help further with more specifically my issue.

"Well, in aom we are setting QuestVar names dynamically not normal var names and I think Quest Vars are stored in some sort of array/table.

In lua, global variables are accessible via the _G table, which gives you a way to name variables on the fly and you can embed lua in C, so that gives you a round-about way of having dynamically named variables.

For example,

texttouse="MyVar"
for i=1,5 do
_G[texttouse..i]=i
end

will give you the variables MyVar1, MyVar2, MyVar3, MyVar4 and MyVar5
"

was the response given to me in another community.


Wed Apr 18, 2012 5:24 pm
Profile
Novice
User avatar

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Post Re: Variables named by program
Ghosto wrote:
I've found what makes me able to do the naming of variables like how i want, maybe someone could assess this and help further with more specifically my issue.

"Well, in aom we are setting QuestVar names dynamically not normal var names and I think Quest Vars are stored in some sort of array/table.

In lua, global variables are accessible via the _G table, which gives you a way to name variables on the fly and you can embed lua in C, so that gives you a round-about way of having dynamically named variables.

For example,

texttouse="MyVar"
for i=1,5 do
_G[texttouse..i]=i
end

will give you the variables MyVar1, MyVar2, MyVar3, MyVar4 and MyVar5
"

I don't think ROBOTC has that feature. ROBOTC is modeled after the C language, but it doesn't have all of the features of the C language...

As sumasmreq said, arrays and/or structs are probably your best options.

//Andrew

_________________
Check out my website! www.RoboDesigners.com

VRC Team 2190

Twitter: @RoboDesigners


Sun Apr 29, 2012 8:59 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Variables named by program
While ROBOTC does not support this directly, it's not impossible to implement it yourself. I don't know if this is what sumasmreq was getting at, but all you need is an array of structs.
Code:
typedef struct{
  string varName;
  int value;
} sVar;

Now, if you want, you could write a hashmap function for ROBOTC (I'm going to attempt to write one now, once finals are over), but it won't be very simple. If you don't want to spend the time writing one, and you don't care too much about computation speed, you could also just store the variables in an array and iterate over each element.
Code:
#define MAX_VARIABLES 100
#define NULL -32768

sVar varStore[MAX_VARIABLES];

void valloc(string name, int value){
  for(int i = 0; i < MAX_VARIABLES; i++){
    if(strlen(varStore[i].varName) == 0){
      varStore[i].varName = name;
      varStore[i].value = value;
      break;
    }
  }
}

void vfree(string name){
  for(int i = 0; i < MAX_VARIABLES; i++){
    if(strcmp(varStore[i].varName, name) == 0){
      varStore[i].varName = "";
      break;
    }
  }
}

int get(string name){
  for(int i = 0; i < MAX_VARIABLES; i++){
    if(strcmp(varStore[i].varName, name) == 0){
      return varStore[i].value;
      break;
    }
  }
  return NULL;
}

Now I haven't tested this code, but you get the idea.

_________________
sudo rm -rf /


Sun Apr 29, 2012 2:24 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Variables named by program
I made a hashmap that you can use to implement this behavior: viewtopic.php?f=15&t=4483

_________________
sudo rm -rf /


Mon Apr 30, 2012 3:38 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.