the original form.
Before the intersection:
1 2
1 2 3
1 2 3 4
1 3
1 3 4
....
Now my output is like this:
2
1
2
1
3
2
1
3
4
1
3
1
3
4
i need to restore output in array and make them become like this:
my expected output that i want:
2 1
2 1 3
2 1 3 4
1 3
1 3 4
..
here is my coding:
for (int i = 0; i < myArray1.Count; i++)
{
foreach (string t in myArray1[i].ToString().Split(' '))
{
//ItemKey.AppendText(t);
hs3.Add(t);
}
var clone = new HashSet(hs2);
hs2.IntersectWith(hs3);
foreach (string y in hs2)
{
ItemKey.AppendText(y);
}
hs2 = clone;
hs3.Clear();
}
What should i need to do...to make my output become like this...hope someone can help me.thanks ya...
Sin YeePosted Apr 7, 2011, 10:55 AM
Sin YeePosted Apr 6, 2011, 7:41 PM
VulpesPosted Apr 6, 2011, 5:28 PM
I've revised my code to start with exactly what you said you saw when you iterated through hs2 and myArray1 (the thread's got so long that I think I must have used some earlier stuff before):
2 1
2 1 3
2 1 3 4
2 1 4
1 3
1 3 4
1 4
2 3
2 3 4
2 4
2 5
3 4
which is exactly what you said you were expecting.
Sin YeePosted Apr 6, 2011, 4:30 PM
Sin YeePosted Apr 6, 2011, 4:24 PM
HashSet
var g = from k in sItem.Keys
orderby sItem[k] descending
select k;
foreach (var k in g)
{
//display3 += k + ":" + sItem[k] + "\r\n";
SingleSupport.AppendText(k + ":" + sItem[k] + "\r\n");
hs1.Add(k);
}
//key and values for comabination
ArrayList myArray1 = new ArrayList();
string text = "";
IDictionaryEnumerator et = itemS.GetEnumerator();
while (et.MoveNext())
{
KeyitemS.AppendText(et.Key.ToString() + ":" + et.Value.ToString() + "\r\n");
myArray1.Add(et.Key.ToString());
}
HashSet
HashSet
for (int i = 0; i < myArray1.Count; i++)
{
foreach (string t in myArray1[i].ToString().Split(' '))
{
hs3.Add(t);
}
var clone = new HashSet
hs2.IntersectWith(hs3);
string[] myArray2 = new string[myArray1.Count];
hs2 = clone;
hs3.Clear();
this is the code that i used to do my intersection
the hs1 is store the single item key and value that already sorted from highest to lowest based on the values.
2:8
1:7
3:7
4:7
5:1
the myArray1 is store the key of combination
1 2
1 2 3
1 2 3 4
1 2 4
1 3
1 3 4
1 4
2 3
2 3 4
2 4
2 5
3 4
i need to do the intersection with these two.
hs3 is store the item from myArray1 after split.
hs2 is store the hs1. after each of the intersection it will reset and intersect with the hs3. hs3 will be cleared after each intersection to store the new next line item.
After that i need to convert back to array and display the output like this
2 1
2 1 3
2 1 3 4
2 1 4
1 3
1 3 4
1 4
2 3
2 3 4
2 4
2 5
3 4
...
this is the output that i want....
VulpesPosted Apr 6, 2011, 4:06 PM
But, anyway, I've extracted both the data and the code into a console app so we can see exactly what's going on. I've had to use a List
The output is:
which looks OK to me.
Sin YeePosted Apr 6, 2011, 3:49 PM
Sin YeePosted Apr 6, 2011, 3:24 PM
output:
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
output:
12 times loops for
1 2
1 2 3
1 2 3 4
1 2 4
1 3
1 3 4
1 4
2 3
2 3 4
2 4
2 5
3 4
VulpesPosted Apr 6, 2011, 3:12 PM
1. hs2 is not a HashSet but something else which has an IntersectWith method.
2. The items in the HashSet contain variable amounts of whitespace after the digit.
3. I'm going completely mad!
I'm tending at the moment towards option #3 but then I can't understand why the code using strarray isn't working.
Can you change the previous code snippet slightly to this and tell me again what you see:
And then finally, do this one as well, so we're absolutely sure what we've got:
Sin YeePosted Apr 6, 2011, 2:50 PM
2
1
2
1
3
2
1
3
4
2
1
4
1
3
1
3
4
1
4
2
3
2
3
4
2
4
2
5
3
4
VulpesPosted Apr 6, 2011, 2:47 PM
Sin YeePosted Apr 6, 2011, 2:37 PM
Sin YeePosted Apr 6, 2011, 2:34 PM
VulpesPosted Apr 6, 2011, 2:27 PM
What I need to know is where is '2121321342141313423234242534' stored?
You said in your last but two post that it was stored in a string array but what's the name of the array because I can't see it in your code?
Sin YeePosted Apr 6, 2011, 2:22 PM
VulpesPosted Apr 6, 2011, 2:10 PM
So, where is your code which stores and, presumably prints out, your output?
If I could see that, then I should be able to figure out what changes are needed to make this work.
Sin YeePosted Apr 6, 2011, 1:32 PM
Sin YeePosted Apr 6, 2011, 11:08 AM
is like this:
2 1
2 1
3
2 1
3 4
....and so on...
the output that i hold before i do this arrangement is 2121321342141313423234242534 (in string array)
not in vertical
2
1
2
1
3
2
1
3
4
2
1
4
2
3
2
3
4
2
4
2
5
3
4
How to solve this problem..Sorry that i trouble you...But i am newbie for c#.So hope you can help me.
VulpesPosted Apr 6, 2011, 10:53 AM
Sin YeePosted Apr 6, 2011, 10:17 AM
Sin YeePosted Apr 6, 2011, 10:17 AM
Sin YeePosted Apr 6, 2011, 9:57 AM
Sin YeePosted Apr 5, 2011, 8:15 PM
here is the code...
String [] Array = new string[myArray1.Count];
String[] strarry;
for (int i = 0; i < myArray1.Count; i++)
{
foreach (string t in myArray1[i].ToString().Split(' '))
{
//ItemKey.AppendText(t);
hs3.Add(t);
}
var clone = new HashSet
hs2.IntersectWith(hs3); // intersection
strarry = hs2.ToArray(); //store back to array
string[] myArray2 = new string[myArray1.Count];
int[] lens = new int[myArray1.Count];
int index = 0;
foreach (string y in myArray1)
{
lens[index] = y.Replace(" ", "").Length;
}
index = 0;
int index2 = 0;
string[] temp = new string[lens[0]];
foreach (string q in strarry)
{
if (index2 < lens[index])
{
temp[index2] = q.ToString();
index2++;
}
else
{
myArray2[index] = String.Join(" ", temp);
index++;
temp = new string[lens[index]];
temp[0] = q.ToString();
index2 = 1;
}
}
myArray2[index] = String.Join(" ", temp);
ItemKey.AppendText(String.Join("\r\n", myArray2));
hs2 = clone;
hs3.Clear();
}
VulpesPosted Apr 5, 2011, 4:26 PM
If the output is stored in a string array rather than a simple string, then my first attempt should work as there's no difference between iterating through an array or a list when using foreach.
As long as there were 14 digits in the original list before the intersection and there are 14 elements in the output string array, then the length shouldn't be a problem.
What the code is doing is working out the length of each string in the original list and storing it in the 'lens' array. It then counts off the appropriate number of digits from the output array, separates them with spaces and then inserts the result in myArray2.
Sin YeePosted Apr 5, 2011, 4:19 PM
Sin YeePosted Apr 5, 2011, 4:13 PM
VulpesPosted Apr 5, 2011, 3:21 PM
Sin YeePosted Apr 5, 2011, 3:00 PM
Sin YeePosted Apr 5, 2011, 2:38 PM
Sin YeePosted Apr 5, 2011, 2:38 PM
VulpesPosted Apr 5, 2011, 2:23 PM