I have a dictionary iteration that uses a for-each comparisment in order to find multiple equalities in a string (fileline).
//
// reads fileLine
//
public void readLine(string fileline)
{
//// turn fileline into desired format
fileline = fileline.ToLower();
fileline = fileline.Trim(StaticData.secondLvlSeperation);
lineElements = fileline.Split(firstLvlSeperation, StringSplitOptions.None);
if (lineElements.Length > 2)
{
alert = createAlert(systemname, diagnoseClass);
// == matched sequences when iterating through dictionary on each line new == //
WordReader.fileLine = fileline;
WordReader.matched = new HashSet<string>();
WordReader.iterationCounter = 0;
WordReader.lineLength = lineElements.Length;
}
else
{
return;
}
foreach (string lineElement in lineElements)
{
string trimmed = lineElement.Trim();
WordReader.dictionaryIteration(trimmed);
}
checkAndAddAlert(alert);
GC.Collect();
}
dictionaryIteration
below works as followed:
There is a dictionary
StaticData.keywords<string, string>
coming from a file that is loaded at start of Form.The dictionary key is a string that holds a keyword which functions as an alert which could (or could not) be found in a file.
The dictionary value is a string that holds a value which is given to the keyword: ('critical', 'error' or 'informatic')
<k,v>
example could be:
"mengbed1 connected" - 'informatic' (full sequence match in fileline)
or
"failed,insert,msstraat4" - 'error' (wildcard, multiple instances required in fileline)
Iterator
'hits' same number as fileline.length- Possibly the match-sequence formed is equal to one of the keys, key-match is found. Key is bound to alert
- IF NO matched are found, we are possibly dealing with a wildcard-key
- Fileline must contain -ALL- desired matched words coming from wildcard key in order to connect wildcard key to alert
- No match of either sides means unknown alert
.
using System.Collections.Generic;
using System.Linq;
namespace Logdog.Processors.Readers
{
class WordReader
{
public static HashSet<string> matched;
public static string fileLine;
public static int lineLength;
public static int iterationCounter = 0;
//
// searches for match sequences in dictionary
//
public static void dictionaryIteration(string wordToCheck)
{
iterationCounter++;
//// match-forming in order to maintain right match sequence after 'hitting' filelength
foreach (KeyValuePair<string, string> entry in StaticData.keywords.Where(ek => !ek.Key.Contains(',')))
{
string keyword = entry.Key;
string[] keywords = keyword.Split();
foreach (string keywordpart in keyword.Split())
{
if (keywordpart.Equals(wordToCheck))
{
matched.Add(keywordpart);
}
}
}
//// at the end of the line
if (iterationCounter.Equals(lineLength))
{
compareMatchedSeq();
}
//// when no equality found at all
if (Linereader.alert.alertType == null)
{
Linereader.alert.alertType = "Unknown";
Linereader.alert.alertKeyword = "Undefined";
}
}
//
// compares the match sequence with dictionary elements
//
private static void compareMatchedSeq()
{
string matchedSeqRegular = string.Join(" ", matched);
// first check on direct match equality
KeyValuePair<string, string> matchedEntry = StaticData.keywords.FirstOrDefault(k => k.Key.Equals(matchedSeqRegular));
if (matchedEntry.Key != null)
{
Linereader.alert.alertKeyword = matchedEntry.Key;
Linereader.alert.alertType = matchedEntry.Value;
}
// if none found, this would mean this sequence is a wildcard type of key
else
{
int matchCounter = 0;
foreach (KeyValuePair<string, string> wildCardEntry in StaticData.keywords.Where(ek => ek.Key.Contains(',')))
{
string[] keywordParts = wildCardEntry.Key.Split(',');
// check if all required matches are qualified
for (int i = 0; i < keywordParts.Length; i++)
{
if (fileLine.Contains(keywordParts[i]))
{
matchCounter++;
}
}
// when all matched
if (matchCounter.Equals(keywordParts.Length))
{
Linereader.alert.alertKeyword = wildCardEntry.Key;
Linereader.alert.alertType = wildCardEntry.Value;
break;
}
}
}
}
}
}
I have the idea that I may make too many for-each loops, (inc. the LINQ usage as well) but when trying other ways around the methods no longer work as desired so this is why I am asking this community if I indeed do have too many loops.
fileLine
andStaticData.keywords
declared? Can you show some sample data? It is difficult to understand what you are doing. \$\endgroup\$static
. \$\endgroup\$