user643003
user643003

Reputation: 11

C# Grouping numbers

I am generating an excel output with a field that has repetitive numbers in each record. The number increments at a change of a value in another field.

Example: Field1     field2

           1         A
           1         A
           2         B
           3         D
           4         F
           4         F
           4         F
           5         R

I want 1 to represent A, 2 represent B, 3 represent D etc and not have a repeat of numbers.

Upvotes: 0

Views: 155

Answers (2)

Francesco De Vittori
Francesco De Vittori

Reputation: 9290

Your question is not very clear, but here are some suggestions:

  • you can generate a column with the numbers (1, 2, 3, etc.) and a second column with the Char formula. Example:
    [A]      [B]
    1        =Char(A1 - 64)
    2        =Char(A2 - 64)
    3        =Char(A3 - 64)

if you have the characters and want to obtain the values you can do the opposite by using the Code function:

    [A]             [B]
    =Code(A1) - 64   A
    =Code(A2) - 64   B
    =Code(A3) - 64   C
  • Otherwise you can directly generate the values for both columns in C# by casting to char (and to int to do the opposite).

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108790

It's a bit hard to interpret your question, but perhaps you want something like this:

Dictionary<string,int> dict=new Dictionary<string,int>();
int counter=1;
int IdOfString(string s)
{
  int i;
  if(dict.TryGetValue(s,i))
    return i;
  else
  {
     i=counter;
     counter++;
     dict.Add(s,i);
     return i;
  }
}

It returns a new number for each unique string you pass into IdOfString

Upvotes: 1

Related Questions