- Home /
how return array name in array
Hi all, I have an array of arrays - I believe it's called jagged array, but I'm not sure. How can I access or return the NAMES of arrays within that array? Not the values. I would like to print out the array names.
string[] groupA = new string[]{name1, name2, name3, name4, name5};
string[] groupB = new string[]{name6, name7, name8, name9, name10};
string[] groupC = new string[]{name11, name12, name13, name14, name15};
string[] groupD = new string[]{name16, name17, name18, name19, name20};
string[] groupE = new string[]{name21, name22, name23, name24, name25};
strging[][] groups = new string[][]{groupA, groupB, groupC, groupD, groupE};
I would like to get for example the third item in groups array by name: So that when I do this:
Debug.Log(groups[2][]);
I would like the program to print out "group C". Not the items in group C.
What do you mean by names of arrays? $$anonymous$$eys? I assume you are working with unityscript then?
Hi Unitraxx - I updated my question. PS: I don't know what "keys" are.
@PaxForce C#'s reflection techniques might make it possible to print the name of a variable, but as soon as you insert groupC
into groups
it becomes just another unnamed array inside of an array of arrays. And don't even bother researching reflection, as it's not supposed to be used for "normal" program$$anonymous$$g. What you're trying to achieve is much better suited by Dictionaries (mapping a string name to an array) or Unitraxx's suggestion of $$anonymous$$ey/Value pairs.
Answer by Unitraxx · Nov 02, 2014 at 12:03 PM
I would look to use List or Dictionary. One suggestion:
List<KeyValuePair<string, string[]>> groups = new List<KeyValuePair<string, string[]>>();
groups.Add(new KeyValuePair<string, string[]>("Group A", new string[]{name1, name2, name3, name4, name5});
groups.Add(new KeyValuePair<string, string[]>("Group B", new string[]{name6, name7, name8, name9, name10});
groups.Add(new KeyValuePair<string, string[]>("Group C", new string[]{name11, name12, name13, name14, name15});
If you would like to print the group name now, you can do:
Debug.Log(groups[2].Key);
If you want to access a group you can do:
Debug.Log(groups[2].Value);
If you would like to access a name in a group:
Debug.Log(groups[2].Value[index]);
I want to learn about $$anonymous$$eys and stuff one day, but right now I need something else. Please tell me how to do it using the script as I have it set up now.
really? I would never thought that such simple task would be impossible in C#. I'll try to check out the $$anonymous$$eys in some tutorial and than I'll get back to your answer and accept it if it works.
And there's nothing special about the $$anonymous$$ey stuff. A $$anonymous$$eyValuePair is simply 2 values combined. With $$anonymous$$ey being the first value, and Value being the second value. (Similar to a Tuple, but limited to 2 values.)
Well it's just not possible with solely the arrays you are using.
Answer by Gardosen · Nov 02, 2014 at 01:25 PM
First of all, i would like to tell you that this way of using arrays is extremly ineffective. i have to agree with Unitraxx that his solution is a better one.
but if you wanna do it realy with our static sourcecode a solution would be the following.
Just add another array to your sourcecode
string[] groupNames = { "groupA", "groupB", "groupC", "groupD", "groupE"};
with this help array, you can use the index for grabbing the array from the toparray and gather informations, or you are using the index on the groupNames array to get the name of the array you defined.
Example:
int indexValue = 2;
int groupId = 1;
string groupAValue = groups[groupId][indexValue];
string groupName = groupNames[groupId];
Debug.Log("Groupname:"+groupName+" / Value:"+groupAValue);
the result would be "Groupname:groupA / Value:a Value"
but i realy want to mention again, you should change soon to the keypair value solution.
Greetings Gardosen
Your answer
Follow this Question
Related Questions
Using a number/brackets in a function name? 1 Answer
Textmesh Username for networkplayers. help. 1 Answer
generating unique names for prefabs within an array 2 Answers
Naming array elements 1 Answer
naming a variable with a number 1 Answer