I have a dictionary as follows:
static Dictionary
where i have 17 string's. For each of 17 string's there is an associated list of 1500 string's. Now what i want to do is: compare strings in all the lists at each and every index.
EX: 1st list 1st element should be compared with all the list's 1st element. Similarly for all the indexes in all the lists.
VulpesPosted Sep 16, 2011, 2:46 PM
StreamWriter ^sw = gcnew StreamWriter(filePath);
// look for empty strings
for each(String ^key in language_string_table->Keys)
{
for(int v = 0; v < 1575; v++)
{
if (language_string_table[key][v] == "") sw->WriteLine("0x00");
}
}
// look for common strings
List
common->Add("");
if(language_string_table->Values->Count > 1)
{
for each(String ^key in language_string_table->Keys)
{
for(int v = 0; v < 1575; v++)
{
bool foundDuplicate = false;
String ^next = language_string_table[key][v]; // next value to compare with other values
if (common->Contains(next)) continue; // already found this one
for(int i = 0; i < 1575; i++)
{
for each(String ^k in language_string_table->Keys)
{
if (language_string_table[k][i] == next && (key != k || v != i))
{
common->Add(next);
foundDuplicate = true;
break; // no point looking any further
}
}
if (foundDuplicate)
{
int numBytes = 2 * next->Length; // all strings are unicode in .NET
sw->WriteLine(String::Format("{0},{1}", numBytes, GetHexString(next)));
break;
}
}
}
}
}
// look for left over strings
for each(String ^key in language_string_table->Keys)
{
for(int v = 0; v < 1575; v++)
{
String ^next = language_string_table[key][v];
if (!common->Contains(next))
{
int numBytes = 2 * next->Length; // all strings are unicode in .NET
sw->WriteLine(String::Format("{0},{1}", numBytes, GetHexString(next)));
}
}
}
sw->Close();
// helper method
String ^GetHexString(String ^s)
{
array
int i = 0;
for each(Char c in s) sa[i++] = String::Format("0x{0:X2}", (int)c);
return String::Join(",", sa);
}
VulpesPosted Sep 19, 2011, 2:23 PM
Karthik AgarwalPosted Sep 19, 2011, 9:41 AM
Karthik AgarwalPosted Sep 16, 2011, 1:04 PM
From the example you stated
1: Common strings are:
dog, cat, duck
2: left over strings are:
lion, tiger swan
Now it will clear for i hope.
VulpesPosted Sep 16, 2011, 12:59 PM
Karthik AgarwalPosted Sep 16, 2011, 12:17 PM
VulpesPosted Sep 16, 2011, 12:13 PM
So, if common strings can occur in the same list, am I right in thinking that a common string doesn't have to appear in every list?
To take an extreme example, if a string only appeared twice in the same list but in no other list, then it would still be regarded as common?
Karthik AgarwalPosted Sep 16, 2011, 12:06 PM
1. I will write a for each loop to generate hexadecimal value after getting decimal value of the same. The for each loop automatically returns 0 as it doesn't contain any characters. Hence i will 0x00 to external file by appending 0x to the string.
2. Ya all the occurrences of empty strings needs to be written to the file
3. The common strings need not be in same index in all the lists to treat that as common strings. In can be any index and in any list even in the same list.
4. Only the hexadecimal value is to be written to the external file.
And the format in which i want to write in external file is as follows
1. If it's an empty string i will write
0x00 for all empty strings
2. If it's a common string i will write as
"no.of bytes occupied by current string, each character hex value separated by comma"
3. and if it's neither empty string nor it didn't match with any other string
"no.of bytes occupied by current string, each character hex value separated by comma"
I guess i answered everything you asked.
VulpesPosted Sep 16, 2011, 11:53 AM
Karthik AgarwalPosted Sep 16, 2011, 11:08 AM
This has become a pretty long discussion which i haven't expected. I thought that if i explain everything you might get confused, hence i explained some part of my project. So issues are coming one after the other. Now what i intended to do completely in my project is as follows:
I have 17 languages(Keys) and each 1575 list of strings(Values) associated for each language. What i need to do exactly with this is:
1. first i need to look for empty Values in all the lists and then if there are any empty strings i should generate hexadecimal value for the same.
2. Second i need to look for common strings i.e common Values in all the lists and generate hexadecimal value for the same.
3. For the left over strings in all the lists i should generate hexadecimal value in the same way.
Note: in the 2nd point which i stated, for all the common strings i should generate hexadecimal value only once.
And i am using stream writer to write this hexadecimal values to output files.
Last post of your's wasn't looking for common strings in the same language list rather it was looking only in other lists.
VulpesPosted Sep 16, 2011, 9:47 AM
Karthik AgarwalPosted Sep 16, 2011, 8:03 AM
VulpesPosted Sep 16, 2011, 7:34 AM
if(language_string_table->Values->Count > 1)
{
for each(String ^key in language_string_table->Keys)
{
for(int v = 0; v < 1500; v++)
{
bool areEqual = true;
String ^next = language_string_table[key][v]; // next value to compare with other lists
for(int i = 0; i < 1500; i++)
{
for each(String ^k in language_string_table->Keys)
{
if (key == k) continue; // only compare with other lists
String ^value = language_string_table[k][i];
if (value != next)
{
areEqual = false;
break;
}
}
if(!areEqual) break; // EDIT
}
if(areEqual)
{
// do something
}
else
{
// do something else
}
}
}
}
Karthik AgarwalPosted Sep 16, 2011, 7:00 AM
VulpesPosted Sep 16, 2011, 6:57 AM
Karthik AgarwalPosted Sep 16, 2011, 6:50 AM
"Are you perhaps saying that you want to test the first element of the first list for equality with all the elements of all the other lists, the second element of the first list for equality with the same set of elements and so on?"
This is want i want to do....
VulpesPosted Sep 16, 2011, 6:46 AM
Karthik AgarwalPosted Sep 16, 2011, 6:11 AM
There is a small issue in the post which i accepted as answer.
The issue is that, the control doesn't come to this place more than once
first = language_string_table[key][0];
hence string doesn't get updated. So all the time the only 1st list 1st element will be compared to all other elements in all other lists. What i was expecting is i should compare 1st list 1st element with all other elements and if it matches then do some task then i should take the 2nd element in the 1st list and compare with all other elements and if it matches then do the same task which i did previously similarly for all the elements in all the lists. Can you help me out.?
Karthik AgarwalPosted Sep 16, 2011, 2:32 AM
Ya i liked the way Vulpes answers all of my questions. Hence so many likes.
Appreciation makes people work more efficiently, what i feel.
Suthish NairPosted Sep 15, 2011, 11:10 AM
VulpesPosted Sep 15, 2011, 7:06 AM
bool areEqual = true;
if(language_string_table->Values->Count > 1)
{
// get the first value from the dictionary
String ^ first;
for each(String ^key in language_string_table->Keys)
{
first = language_string_table[key][0];
break;
}
for(int i = 0; i < 1500; i++)
{
for each(String ^key in language_string_table->Keys)
{
String ^value = language_string_table[key][i];
if (value != first)
{
areEqual = false;
break;
}
}
}
}
if(areEqual)
{
// do something
}
else
{
// do something else
}
Karthik AgarwalPosted Sep 15, 2011, 6:45 AM
That's a perfect answer. Can you tell me an extension of the same question which i asked above?
What should i do if had to compare all the elements in all the lists.
Ex: just like comparing 1st element of the 1st list with all the elements of all the lists and should judge whether they are same or not?
Thanks & Regards,
P.Karthik
VulpesPosted Sep 15, 2011, 5:28 AM