Cumulatively add values to Dictionary - c#

I am trying to add values to a dictionary as they get determined based on certain conditions. The app has to loop through each line and once a certain condition has been met then a value must be added to the dictionary. Here is the code that is task with looping through the lines and determine values to be added.
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
//AddErrorFinesToFile(line, fineNumber);
AddFinesToDictonary(fineNumber, line);
continue;
}
}
Then below is the actual method signature and its code, in this method I am simply trying to add values to the dictionary as they come.
public Dictionary<string, string> AddFinesToDictonary(string fineNumber, string errorLine)
{
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
erroredLines.Add(fineNumber, errorLine);
return erroredLines;
}
The only problem that seems to arise here is, only the latest value gets added to the dictionary, meaning the previous added value gets overwritten.

Make erroredLines as global scope.
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
//AddErrorFinesToFile(line, fineNumber);
AddFinesToDictonary(fineNumber, line);
continue;
}
}
public void AddFinesToDictonary(string fineNumber, string errorLine)
{
erroredLines.Add(fineNumber, errorLine);
// return erroredLines;
}
And also no need to return erroredLines dictionary.

What about this;
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
erroredLines.Add(fineNumber, line);
continue;
}
}
after foreach you can use erroredLines dictionary.

The reason is every time you add data to the Directory you create a new one instead of adding data to the exists one
There are two choices you could make:
make a Dictionary out of the function
pass the Dictionary as an out ref to the function
#Hameed Syed 's answer has already given the first one (option 1).
Here is how you could pass the Dictionary as a ref parameter (out) to the function (option 2):
public void AddFinesToDictonary(out Dictionary<string,string>dict, string fineNumber, string errorLine)
{
dict.Add(fineNumber, errorLine);
}

Related

how to find number of array list from contains string

how to find number of array list same as contains string, i want make contains string "guess1" get "answer1" and "guess2" get "answer2". how to find n number of array same as contains string?
public class FindContainsText : MonoBehaviour
{
public Text text;
public InputField intext;
List<string> guess = new List<string>();
List<string> answer = new List<string>();
private int n;
void Start()
{
guess.Add("test1");
guess.Add("test2");
answer.Add("answer1");
answer.Add("answer2");
}
// Update is called once per frame
void Update()
{
foreach (string x in guess)
{
if (intext.text.ToLower().Contains(x.ToLower()))
{
text.text = answer[n];
return;
}
}
text.text = "not found";
}
}
Then you should use Dictionary type.
private Dictionary<string, string> guessAnswerDict = new Dictionary<string, string>();
private void Start()
{
guessAnswerDict["test1"] = "answer1";
guessAnswerDict["test2"] = "answer2";
}
You can check whether the test exists in Dictionary with
guessAnswerDict.Contains("test1");
And get the answer value with
var answer = guessAnswerDict["test1"];
It will throw an exception when there are no key in dictionary, so you have to check it with Contains.
Of course you can merge these two with TryGetValue, like
string answer;
guessAnswerDict.TryGetValue("test1", out answer);
// Totally identical!!
guessAnswerDict.TryGetValue("test1", out var answer);
TryGetValue will return false if there are no key in the dictionary.
BTW, although C#'s default access modifier is private, it's always good to explicitly write it's private, which will increase the readability of your code :)

Read ini sections with no values and append it to a dictionary

I have the following ini file with sections and keys but no values asigned:
[core]
bul_gravel_heli
ent_dst_concrete_large
bul_wood_splinter
[cut_armenian1]
cs_arm2_muz_smg
cs_ped_foot_dusty
What I want to do is:
Read all the secions and vaues.
Store them in a dictionary in the format:
{section: {key1, key2, key3, key4, etc}
Now the problem is that I can't find anywhere an example of ini file reading without values, all the results I've found are for reading ini files without sections.
To give a brief of what I want to do with the stored dictionary is this:
There's a function public void AddList(string listName, List<dynamic> list) and for each one of the dictionary keys and values I want to create the method. I already know that I can use for loops but I'm stuck at parsing the ini file.
Well, a simple foreach loop should do:
private static Dictionary<string, List<string>> IniToDictionary(IEnumerable<string> lines) {
Dictionary<string, List<string>> result =
new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
string category = "";
foreach (string line in lines) {
string record = line.Trim();
if (string.IsNullOrEmpty(record) || record.StartsWith("#"))
continue;
else if (record.StartsWith("[") && record.EndsWith("]"))
category = record.Substring(1, record.Length - 2);
else {
int index = record.IndexOf('=');
string name = index > 0 ? record.Substring(0, index) : record;
if (result.TryGetValue(category, out List<string> list))
list.Add(name);
else
result.Add(category, new List<string>() { name});
}
}
return result;
}
If you want to process a file:
Dictionary<string, List<string> result = IniToDictionary(File
.ReadLines(#"c:\MyIniFile.ini"));
Let's have a look (at test input):
Console.Write(tring.Join(Environment.NewLine, result
.Select(pair => $"{pair.Key,-15} : [{string.Join(", ", pair.Value)}]")));
Outcome:
core : [bul_gravel_heli, ent_dst_concrete_large, bul_wood_splinter]
cut_armenian1 : [cs_arm2_muz_smg, cs_ped_foot_dusty]

Getting a specific key from within a hashtable

So I have this Hashtable
Hashtable Months = new Hashtable();
Months.Add(0, "JANUARY");
Months.Add(1, "FEBRUARY");
Months.Add(2, "MARCH");
Months.Add(3, "APRIL");
Months.Add(4, "MAY");
Months.Add(5, "JUNE");
Months.Add(6, "JULY");
Months.Add(7, "AUGUST");
Months.Add(8, "SEPTEMBER");
Months.Add(9, "OCTOBER");
Months.Add(10, "NOVEMBER");
Months.Add(11, "DECEMBER");
I would like for the user to enter a month e.g."May" the be able to retrieve index[4] from an array within my program.
string Month = Console.ReadLine();
Basically to retrieve the index from the number of the corresponding Month entered.
Try this
var key = Months.Keys.Cast<int>().FirstOrDefault(v => Months[v] == "MAY");
Note: Don't forget to include this namespace - using System.Linq;
Get elements from your Hashtable in DictionaryEntry format
foreach (DictionaryEntry e in Months)
{
if ((string)e.Value == "MAY")
{
//get the "index" with e.Key
}
}
You can perform it just using a loop;
public List<string> FindKeys(string value, Hashtable hashTable)
{
var keyList = new List<string>();
IDictionaryEnumerator e = hashTable.GetEnumerator();
while (e.MoveNext())
{
if (e.Value.ToString().Equals(value))
{
keyList.Add(e.Key.ToString());
}
}
return keyList;
}
Usage;
var items = FindKeys("MAY",Months);
If you want to lookup the index from the month's name, a Dictionary<string, int> would be more suitable. The reason why I've swapped the parameters is becuase if you only want to lookup the index, and not also the other way around, this will be much faster.
You should declare the dictionary as case-insensitive so that it detects for instance may, May, mAy and MAY as the same thing:
Dictionary<string, int> Months = new Dictionary<string, int>(StringComparison.OrdinalIgnoreCase);
Then just use its TryGetValue() method whenever you want to get the month index:
int MonthIndex = 0;
if(Months.TryGetValue(Month, out MonthIndex)) {
//Month was correct, continue your code...
else {
Console.WriteLine("Invalid month!");
}

Argument Exception "Item with Same Key has already been added"

I keep getting an error with the following code:
Dictionary<string, string> rct3Features = new Dictionary<string, string>();
Dictionary<string, string> rct4Features = new Dictionary<string, string>();
foreach (string line in rct3Lines)
{
string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);
rct3Features.Add(items[0], items[1]);
////To print out the dictionary (to see if it works)
//foreach (KeyValuePair<string, string> item in rct3Features)
//{
// Console.WriteLine(item.Key + " " + item.Value);
//}
}
The error throws an ArgumentException saying,
"An item with the same key has already been added."
I am unsure after several Google searches how to fix this.
Later in the code I need to access the dictionary for a compare function:
Compare4To3(rct4Features, rct3Features);
public static void Compare4To3(Dictionary<string, string> dictionaryOne, Dictionary<string, string> dictionaryTwo)
{
//foreach (string item in dictionaryOne)
//{
//To print out the dictionary (to see if it works)
foreach (KeyValuePair<string, string> item in dictionaryOne)
{
Console.WriteLine(item.Key + " " + item.Value);
}
//if (dictionaryTwo.ContainsKey(dictionaryOne.Keys)
//{
// Console.Write("True");
//}
//else
//{
// Console.Write("False");
//}
//}
}
This function isn't completed, but I am trying to resolve this exception. What are the ways I can fix this exception error, and keep access to the dictionary for use with this function? Thank you
This error is fairly self-explanatory. Dictionary keys are unique and you cannot have more than one of the same key. To fix this, you should modify your code like so:
Dictionary<string, string> rct3Features = new Dictionary<string, string>();
Dictionary<string, string> rct4Features = new Dictionary<string, string>();
foreach (string line in rct3Lines)
{
string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);
if (!rct3Features.ContainsKey(items[0]))
{
rct3Features.Add(items[0], items[1]);
}
////To print out the dictionary (to see if it works)
//foreach (KeyValuePair<string, string> item in rct3Features)
//{
// Console.WriteLine(item.Key + " " + item.Value);
//}
}
This simple if statement ensures that you are only attempting to add a new entry to the Dictionary when the Key (items[0]) is not already present.
If you want "insert or replace" semantics, use this syntax:
A[key] = value; // <-- insert or replace semantics
It's more efficient and readable than calls involving "ContainsKey()" or "Remove()" prior to "Add()".
So in your case:
rct3Features[items[0]] = items[1];
As others have said, you are adding the same key more than once. If this is a NOT a valid scenario, then check Jdinklage Morgoone's answer (which only saves the first value found for a key), or, consider this workaround (which only saves the last value found for a key):
// This will always overwrite the existing value if one is already stored for this key
rct3Features[items[0]] = items[1];
Otherwise, if it is valid to have multiple values for a single key, then you should consider storing your values in a List<string> for each string key.
For example:
var rct3Features = new Dictionary<string, List<string>>();
var rct4Features = new Dictionary<string, List<string>>();
foreach (string line in rct3Lines)
{
string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);
if (!rct3Features.ContainsKey(items[0]))
{
// No items for this key have been added, so create a new list
// for the value with item[1] as the only item in the list
rct3Features.Add(items[0], new List<string> { items[1] });
}
else
{
// This key already exists, so add item[1] to the existing list value
rct3Features[items[0]].Add(items[1]);
}
}
// To display your keys and values (testing)
foreach (KeyValuePair<string, List<string>> item in rct3Features)
{
Console.WriteLine("The Key: {0} has values:", item.Key);
foreach (string value in item.Value)
{
Console.WriteLine(" - {0}", value);
}
}
To illustrate the problem you are having, let's look at some code...
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("Key1", "Value1"); // Works fine
test.Add("Key2", "Value2"); // Works fine
test.Add("Key1", "Value3"); // Fails because of duplicate key
The reason that a dictionary has a key/value pair is a feature so you can do this...
var myString = test["Key2"]; // myString is now Value2.
If Dictionary had 2 Key2's, it wouldn't know which one to return, so it limits you to a unique key.
That Exception is thrown if there is already a key in the dictionary when you try to add the new one.
There must be more than one line in rct3Lines with the same first word. You can't have 2 entries in the same dictionary with the same key.
You need to decide what you want to happen if the key already exists - if you want to just update the value where the key exists you can simply
rct3Features[items[0]]=items[1]
but, if not you may want to test if the key already exists with:
if(rect3Features.ContainsKey(items[0]))
{
//Do something
}
else
{
//Do something else
}
I suggest .NET's TryAdd:
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.tryadd?view=net-7.0
I suggest a extension method for environments where .NET's TryAdd is not available:
public static class DictionaryUtils
{
/// <summary>
/// Prevents exception "Item with Same Key has already been added".
/// </summary>
public static void TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
}
}
}
Clear the dictionary before adding any items to it. I don't know how a dictionary of one object affects another's during assignment but I got the error after creating another object with the same key,value pairs.
NB:
If you are going to add items in a loop just make sure you clear the dictionary before entering the loop.

Using C# Dictionary to parse log file

I am trying to parse a rather long log file and creating a better more manageable listing of issues.
I am able to read and parse out the individual log line by line, but what I need to do is display only unique entries, as some errors occur more often than others and are always recorded with identical text.
What I was going to try to do was create a Dictionary object to hold each unique entry and as I work through the log file, search the Dictionary object to see if the same values are already in there.
Here is a crude sample of the code I have (a work in progress, I hope I have all syntax right) that does not work. For some reason this script never sees any distinct entries (if statement never passes):
string[] rowdta = new string[4];
Dictionary<string[], int> dict = new Dictionary<string[], int>();
int ctr = -1;
if (linectr == 1)
{
ctr++;
dict.Add(rowdta, ctr);
}
else
{
foreach (KeyValuePair<string[], int> pair in dict)
{
if ((pair.Key[1] != rowdta[1]) || (pair.Key[2] != rowdta[2])| (pair.Key[3] != rowdta[3]))
{
ctr++;
dict.Add(rowdta, ctr);
}
}
}
Some sample data:
First line
rowdta[0]="ErrorType";
rowdta[1]="Undefined offset: 0";
rowdta[2]="/url/routesDisplay2.svc.php";
rowdta[3]="Line Number 5";
2nd line
rowdta[0]="ErrorType";
rowdta[1]="Undefined offset: 0";
rowdta[2]="/url/routesDisplay2.svc.php";
rowdta[3]="Line Number 5";
3rd line
rowdta[0]="ErrorType";
rowdta[1]="Undefined variable: fvmsg";
rowdta[2]="/url/processes.svc.php";
rowdta[3]="Line Number 787";
So, with this, the Dictionary will have 2 items in it, first line and 3rd line.
I have also tried this with the following which nalso does not find any variations in the log file text.
if (!dict.ContainsKey(rowdta)) {}
Can someone please help me get this syntax right? I am just a newbie at C# but this should be relatively straightforward. As always, I am thinking that this should be enough information to get the conversation started. If you want/need more detail, please let me know.
Either create a wrapper for your strings which implements IEquatable.
public class LogFileEntry :IEquatable<LogFileEntry>
{
private readonly string[] _rows;
public LogFileEntry(string[] rows)
{
_rows = rows;
}
public override int GetHashCode()
{
return
_rows[0].GetHashCode() << 3 |
_rows[2].GetHashCode() << 2 |
_rows[1].GetHashCode() << 1 |
_rows[0].GetHashCode();
}
#region Implementation of IEquatable<LogFileEntry>
public override bool Equals(Object obj)
{
if (obj == null)
return base.Equals(obj);
return Equals(obj as LogFileEntry);
}
public bool Equals(LogFileEntry other)
{
if(other == null)
return false;
return _rows.SequenceEqual(other._rows);
}
#endregion
}
Then use that in your dictionary:
var d = new Dictionary<LogFileEntry, int>();
var entry = new LogFileEntry(rows);
if( d.ContainsKey(entry) )
{
d[entry] ++;
}
else
{
d[entry] = 1;
}
Or create a custom comparer similar to that proposed by #dasblinkenlight and use as follows
public class LogFileEntry
{
}
public class LogFileEntryComparer : IEqualityComparer<LogFileEntry>{ ... }
var d = new Dictionary<LogFileEntry, int>(new LogFileEntryComparer());
var entry = new LogFileEntry(rows);
if( d.ContainsKey(entry) )
{
d[entry] ++;
}
else
{
d[entry] = 1;
}
The reason that you see the problem is that an array of strings cannot be used as a key in a dictionary without supplying a custom IEqualityComparer<string[]> or writing a wrapper around it.
EDIT Here is a quick and dirty implementation of a custom comparer:
private class ArrayEq<T> : IEqualityComparer<T[]> {
public bool Equals(T[] x, T[] y) {
return x.SequenceEqual(y);
}
public int GetHashCode(T[] obj) {
return obj.Sum(o => o.GetHashCode());
}
}
Here is how you can use it:
var dd = new Dictionary<string[], int>(new ArrayEq<string>());
dd[new[] { "a", "b" }] = 0;
dd[new[] { "a", "b" }]++;
dd[new[] { "a", "b" }]++;
Console.WriteLine(dd[new[] { "a", "b" }]);
The problem is that array equality is reference equality. In other words, it does not depend on the values stored in the array, it depends only on the identity of the array.
Some solutions
use Tuple to hold the row data
use an anonymous type to hold the row data
create a custom type to hold the row data, and, if it is a class, override Equals and GetHashCode.
create a custom implementation of IEqualityComparer to compare the arrays according to their values, and pass that to the dictionary when you create it.

Categories