user3785803
user3785803

Reputation: 41

Subarrays[][] from jagged Array[][]

I have an array
string[][] where data will look like this:

p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,g,f,n,f,w,b,k,t,e,s,s,w,w,p,w,o,e,n,a,g
e,x,y,y,t,a,f,c,b,n,e,c,s,s,w,w,p,w,o,p,k,n,g
e,b,s,w,t,a,f,c,b,g,e,c,s,s,w,w,p,w,o,p,k,n,m
e,b,y,w,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,s,m
p,x,y,w,t,p,f,c,n,p,e,e,s,s,w,w,p,w,o,p,k,v,g
.
.
.

colums are Attributes (eg. 1st col has 2 values (p,e) I'm trying to create sub arrays based on attribute values eg.

subarray1
p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u
p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u

subarray2
e,x,s,g,f,n,f,w,b,k,t,e,s,s,w,w,p,w,o,e,n,a,g
e,x,y,y,t,a,f,c,b,n,e,c,s,s,w,w,p,w,o,p,k,n,g
e,b,s,w,t,a,f,c,b,g,e,c,s,s,w,w,p,w,o,p,k,n,m
e,b,y,w,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,s,m
.
.

i tryed with this:

 public string[][] subSets2(string[][] _dataSet, int AttributeID, int value)
        {
            string[][] SS=new string[_dataSet.Length][];
            List<string> values=Transpose(_dataSet)[AttributeID].Distinct().ToList();
            int t= 0;
            string[][] tempSS = Transpose(_dataSet);
            for (int i= 0;i< _dataSet.Length;i++)
            {
                SS[t]= new string[_dataSet[i].Count()];
                for (int j = 0; j<_dataSet[i].Count() ; j++)
                {
                    if (_dataSet[i][j].Equals(values[value]) && AttributeID== j)
                    {
                        SS[t][j] = _dataSet[i][j];
                            t++;
                    }
                }
            }
            return SS;
        }

Upvotes: 0

Views: 272

Answers (2)

Zohar Peled
Zohar Peled

Reputation: 82474

I'm not sure I understand the question, but if I do, then you want to get a jagged array that contains all the arrays that starts with a specific string ("p" or "e").
If that is the case, you can simply use linq's where extension method:

public string[][] subSets(string[][] _dataSet, string valueOfFirstCell)
{
    return _dataSet.Where(d => d[0] == valueOfFirstCell).ToArray();
}

Upvotes: 0

Roland Mai
Roland Mai

Reputation: 31077

If you want to create sub arrays based on a given column you can use linq as follows:

var subarrays = _dataSet.GroupBy(r => r[0]).Select(r => r.ToArray()).ToArray();

The r[0] refers to the first item in each array. You can change the index to group by a different column.

Upvotes: 2

Related Questions