Arrays

P

plork

hi all i have a method that takes an array

e.g. public void Method (string [] array)

the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length

a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"


If i do this for (int i = 0; i < array.Length; i++)

i can see the length of the whole array

But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results
 
N

Nicholas Paldino [.NET/C# MVP]

In this case, an array might not be the best idea, since what you have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the data
in a way that goes beyond simple access through an indexer.

I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.

If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to get
"rows" and then process that.

Unless, of course, these are just single strings (I read it as multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.
 
P

Pete Kane

Hi there ( off the top of my head) , from within your loop
for (int i = 0; i < array.Length; i++)
{
string s[];
s = array.Tostring().Split(",");
Console.WriteLine(s[0].Length);
}
 
M

Marc Gravell

I assume you mean a string[][] as per last time you asked?

What framework version are you using? If you are using .NET 3.5 / C#
3, then LINQ may be your friend (below). Otherise build a dictionary
of lists (Dictionary<string, List<string[]>> and loop (either creating
new or adding to the existing list).

static void Method(string[][] array)
{
foreach (var grp in array.GroupBy(x => x[0]).OrderBy(grp
=> grp.Key))
{
Console.WriteLine("{0}: {1}", grp.Key, grp.Count());
foreach (string[] row in grp)
{
Console.WriteLine("\t{0} {1} {2}", row);
}
}
}

static void Main(string[] args)
{
string[][] data = {
new[] {"1000", "aaa", "some text"},
new[] {"1000", "bbb", "some text"},
new[] {"1000", "ccc", "some text"},
new[] {"2000", "ddd", "some text"},
new[] {"2000", "eee", "some text"},
new[] {"3000", "fff", "some text"},
new[] {"3000", "ggg", "some text"},
new[] {"3000", "hhh", "some text"},
new[] {"4000", "iii", "some text"},
new[] {"5000", "jjj", "some text"},
new[] {"6000", "kkk", "some text"}
};
Method(data);
}
 
P

plork

In this case, an array might not be the best idea, since what you have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the data
in a way that goes beyond simple access through an indexer.

I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.

If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to get
"rows" and then process that.

Unless, of course, these are just single strings (I read it as multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)






hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -

- Show quoted text -



could you give me an example of parsing them out?
 
P

plork

In this case, an array might not be the best idea, since what you have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the data
in a way that goes beyond simple access through an indexer.

I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.

If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to get
"rows" and then process that.

Unless, of course, these are just single strings (I read it as multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)






hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -

- Show quoted text -



Sorry they are muiliple strings for each row.

I don't have any control how the array is formed as the array is the
retrun from an external webservice call
 
M

Marc Gravell

Here's the same without using LINQ. In my opinion the LINQ version
does a much better job of expressing our intent, rather than bogging
down in implementation details.

Marc

static void Method2(string[][] data)
{
Dictionary<string, List<string[]>> groups = new
Dictionary<string, List<string[]>>();
foreach (string[] row in data)
{
string key = row[0];
List<string[]> contents;
if (!groups.TryGetValue(key, out contents)) {
contents = new List<string[]>();
groups.Add(key, contents);
}
contents.Add(row);
}

// *** note: no guarantee here about the order of keys!
// (it isn't guaranteed to be the order in which we found
them)

foreach (KeyValuePair<string, List<string[]>> grp in
groups)
{
Console.WriteLine("{0}: {1}", grp.Key,
grp.Value.Count);
foreach (string[] row in grp.Value)
{
Console.WriteLine("\t{0} {1} {2}", row);
}
}
}
 
P

plork

In this case, an array might not be the best idea, since what you have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the data
in a way that goes beyond simple access through an indexer.
I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.
If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to get
"rows" and then process that.
Unless, of course, these are just single strings (I read it as multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.
news:9476b215-fc22-46a7-8acc-6d7d6d44a8ae@n20g2000hsh.googlegroups.com...
hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -
- Show quoted text -

could you give me an example of parsing them out?- Hide quoted text -

- Show quoted text -



Sorry yes they are multiple strings in eech row
 
N

Nicholas Paldino [.NET/C# MVP]

Well, what is the format that the strings are in? If you were to assign
one of these strings programmatically to a variable, what would it be?

For example, is one of the strings:

string s = "\"1000\", \"aaa\", \"some text\"";

Or some other format?

If it is the above, then you should look for a CSV parser (which I
assume the above is). If you know that you won't contain commas in the
quoted sections, then you could just use the Split method on the string
class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

plork said:
In this case, an array might not be the best idea, since what you
have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the
data
in a way that goes beyond simple access through an indexer.

I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.

If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to
get
"rows" and then process that.

Unless, of course, these are just single strings (I read it as
multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)






hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -

- Show quoted text -



could you give me an example of parsing them out?
 
P

plork

In this case, an array might not be the best idea, since what you have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the data
in a way that goes beyond simple access through an indexer.

I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.

If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to get
"rows" and then process that.

Unless, of course, these are just single strings (I read it as multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)






hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -

- Show quoted text -


Sorry, yes they are muliple strings for each row
 
P

plork

Well, what is the format that the strings are in? If you were to assign
one of these strings programmatically to a variable, what would it be?

For example, is one of the strings:

string s = "\"1000\", \"aaa\", \"some text\"";

Or some other format?

If it is the above, then you should look for a CSV parser (which I
assume the above is). If you know that you won't contain commas in the
quoted sections, then you could just use the Split method on the string
class.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




In this case, an array might not be the best idea, since what you
have
really needs to be stored in a multi-dimensional array. It's not that
multi-dimensional arrays are bad, but you have a need to work with the
data
in a way that goes beyond simple access through an indexer.
I would recommend that you store this data in a DataTable, and then
place a DataView on the DataTable on the id field, filtering for the rows
that you want. Then, you can cycle through the rows in the DataView, and
process them accordingly.
If you are using .NET 3.5/C# 3.0, you could use LINQ directly on the
array, but you would have to perform a grouping on the elements first to
get
"rows" and then process that.
Unless, of course, these are just single strings (I read it as
multiple
strings for each "row"), in which case, I suggest parsing them out and
working with that data.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -
- Show quoted text -
could you give me an example of parsing them out?- Hide quoted text -

- Show quoted text -



if i was to assign a variable to say a string then i would do this

ArrayResults arrayResults = array;

string aaa = array,Title;
string bbb = array.description
 
B

Ben Voigt [C++ MVP]

plork said:
hi all i have a method that takes an array

e.g. public void Method (string [] array)

the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length

No they aren't, for the simple reason that each element of a string[] is a
single string, not a tuple (id, title, description). Perhaps you instead
want to pass an array of a user-defined type?
a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"


If i do this for (int i = 0; i < array.Length; i++)

i can see the length of the whole array

But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results
 
P

plork

hi all i have a method that takes an array
e.g. public void Method (string [] array)
the contents of array are somthing like below, i don't know what the
length will be be until i do a array.Length

No they aren't, for the simple reason that each element of a string[] is a
single string, not a tuple (id, title, description). Perhaps you instead
want to pass an array of a user-defined type?




a snippet of results is
id title description
0] = "1000", "aaa", "some text"
[1] = "1000", "bbb", "some text"
[2] = "1000", "ccc", "some text"
[3] = "2000", "ddd", "some text"
[4] = "2000", "eee", "some text"
[5] = "3000", "fff", "some text"
[6] = "3000", "ggg", "some text"
[7] = "3000", "hhh", "some text"
[8] = "4000", "iii", "some text"
[9] = "5000", "jjj", "some text"
[10] = "6000", "kkk", "some text"
If i do this for (int i = 0; i < array.Length; i++)
i can see the length of the whole array
But is anyone able to show me how i get the length of id? e.g. I want
to see how many results have an id of e.g. 1000 or id of 2000
etc ....and then loop through those results- Hide quoted text -

- Show quoted text -

when i do an quick watch in vissual studio the vlaues are like below

Array Result value
|-id "1000"
|-title "aaa"
|-description "some text"
 
M

Marc Gravell

so it sounds like you actually have something like:

class ArrayResult {
public string id {get;set;}
public string title {get;set;}
public string description {get;set;}
}

and an array of such - i.e. ArrayResult[]

In which case, the LINQ approach becomes as follows (similar changes
to Dictionary approach):

static void Method(ArrayResult[] results) {
foreach (var grp in
from result in results
group result by result.id) {

Console.WriteLine("{0}: {1}", grp.Key, grp.Count());
foreach (ArrayResult row in grp) {
Console.WriteLine("\t{0} {1} {2}", row.id,
row.title, row.description);
}
}
}

Is this closer?
 
M

Marc Gravell

Dictionary based code follows - but I agree with Jon; some real code
(even if it doesn't work) could save us all a lot of wasted time
here ;-p

static void Method2(ArrayResult[] results)
{
Dictionary<string, List<ArrayResult>> groups = new
Dictionary<string, List<ArrayResult>>();
foreach (ArrayResult row in results)
{
List<ArrayResult> contents;
if (!groups.TryGetValue(row.id, out contents)) {
contents = new List<ArrayResult>();
groups.Add(row.id, contents);
}
contents.Add(row);
}


// *** note: no guarantee here about the order of keys!
// (it isn't guaranteed to be the order in which we found
them)


foreach (KeyValuePair<string, List<ArrayResult>> grp in
groups)
{
Console.WriteLine("{0}: {1}", grp.Key,
grp.Value.Count);
foreach (ArrayResult row in grp.Value) {
Console.WriteLine("\t{0} {1} {2}", row.id,
row.title, row.description);
}
}
}
 
P

plork

Dictionary based code follows - but I agree with Jon; some real code
(even if it doesn't work) could save us all a lot of wasted time
here ;-p

static void Method2(ArrayResult[] results)
{
Dictionary<string, List<ArrayResult>> groups = new
Dictionary<string, List<ArrayResult>>();
foreach (ArrayResult row in results)
{
List<ArrayResult> contents;
if (!groups.TryGetValue(row.id, out contents)) {
contents = new List<ArrayResult>();
groups.Add(row.id, contents);
}
contents.Add(row);
}

// *** note: no guarantee here about the order of keys!
// (it isn't guaranteed to be the order in which we found
them)

foreach (KeyValuePair<string, List<ArrayResult>> grp in
groups)
{
Console.WriteLine("{0}: {1}", grp.Key,
grp.Value.Count);
foreach (ArrayResult row in grp.Value) {
Console.WriteLine("\t{0} {1} {2}", row.id,
row.title, row.description);
}
}
}



I'm calling an external web service method and it retruns a complex
type which is an array


for (int i = 0; i < array.Length; i++)
{
ComplexType results = array ;

each results looks like this when i look in visual studio
Array Result value
|-id "1000"
|-title "aaa"
|-description "some text"

all i want to do is loop through the 'array' retruned from the
webserivce call and group the 1000's together, 2000's together etc

i thought i could do this

for (int i = 0; i < array.Length; i++)
{
ComplexType results = array ;

if (results.id = "1000")
{
create another array
ComplexType 1000Array = new ComplexType ();
1000Array.add - but when i click CTRL+Space next to 1000Array i don't
get Add in the intellisense
}
 
P

plork

In that case you don't have an array of strings.

If you could just post a short but *complete* example, we could avoid
all the guesswork.

Seehttp://pobox.com/~skeet/csharp/complete.html

Jon




If i don't have an array of strings what so i have ?
 
C

Chris Dunaway

string key = row[0];
List<string[]> contents;
if (!groups.TryGetValue(key, out contents)) {

I don't know if it matters, but why not just use the ContainsKey
method instead of TryGetValue?

if (!groups.ContainsKey(key)) {
}

To me, that is much more clear about what is intended.

Cheers.

Chris
 
J

Jon Skeet [C# MVP]

plork said:
If i don't have an array of strings what so i have ?

I don't know. You've got the code in front of you, I haven't. That's
why we're asking you to post the code.
 

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

Similar Threads

array of arrays 2
Compare cells in different worksheets 3
Group/Sort records in Excel 1
Excel Issue 1
EXCEL issue 3
Lookup with loop 3
How to Fill Empty Cells with Data from Previous Records 1
row comparison 5

Top