poss to use strings to index?

  • Thread starter Thread starter Glenn
  • Start date Start date
G

Glenn

Hello again,

New question, is it possible to do anything similar to PHP where
you're able to use a string as an index to another?

Say you're reading a file of different types of fruit, and wish to
increment at the same time, so you have something like :
fruit[infruit]++;
as you get in new fruit, you have a new column/row of data, basically.
I figure that I can just have a routine that will return the index for
each type of item/fruit, whatever.. was just wondering of the PHP
method was possible at all.

Thanks :)
Glenn
 
Glenn said:
Hello again,

New question, is it possible to do anything similar to PHP where
you're able to use a string as an index to another?

Say you're reading a file of different types of fruit, and wish to
increment at the same time, so you have something like :
fruit[infruit]++;
as you get in new fruit, you have a new column/row of data, basically.
I figure that I can just have a routine that will return the index for
each type of item/fruit, whatever.. was just wondering of the PHP
method was possible at all.

Thanks :)
Glenn

What you are looking for is a dictionary. Example:

Dictionary<string, int> fruit = new Dictionary<string, int>;

string infruit = "apple";
if (fruit.ContainsKey(infruit)) {
fruit[infruit]++;
} else {
fruit.Add(infruit, 1);
}

Console.WriteLine("Apples: " + (fruit.ContainsKey("apple") ?
fruit["apple"] : 0).ToString());
 
Glenn said:
Hello again,

New question, is it possible to do anything similar to PHP where
you're able to use a string as an index to another?

Say you're reading a file of different types of fruit, and wish to
increment at the same time, so you have something like :
fruit[infruit]++;
as you get in new fruit, you have a new column/row of data, basically.
I figure that I can just have a routine that will return the index for
each type of item/fruit, whatever.. was just wondering of the PHP
method was possible at all.

Thanks :)
Glenn

What you are looking for is a dictionary. Example:

Dictionary<string, int> fruit = new Dictionary<string, int>;

string infruit = "apple";
if (fruit.ContainsKey(infruit)) {
fruit[infruit]++;
} else {
fruit.Add(infruit, 1);
}

Console.WriteLine("Apples: " + (fruit.ContainsKey("apple") ?
fruit["apple"] : 0).ToString());

Most excellent.. Thank you :)
 

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

Back
Top