Removing values in a dictionary [modified]
-
I think what Luc means is: use the "code block" widget to presever your formatting on your original code. that would produce:
if(!mail)
{
if ((text != ""))
{
int indexofTO = text.IndexOf("to");
int indexofSLOT = text.IndexOf("Slot");
if (indexofSLOT < indexofTO) //load
{
...
}
}
}which I think you will agree is easier to read than:
if(!mail)
{
if ((text != ""))
{
int indexofTO = text.IndexOf("to");
int indexofSLOT = text.IndexOf("Slot");
if (indexofSLOT < indexofTO) //load
{
...
}
}
}Just putting the tags around your already posted code doesn't do that - try the "Preview" button when you add tags.
All those who believe in psycho kinesis, raise my hand.
right (actually this is the left side) :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
I have a dictionary call timeDate. where DriveDT is an object. static Dictionary<string, DriveDT> timeDate = new Dictionary<string, DriveDT>(); I want to remove the value from the dictionary, the line im using is in the section else //unload But the line isnt working. timeDate.Remove(key);
private static void extractSE(string filePath) { StreamReader textIn = new StreamReader(filePath); do { string text = textIn.ReadLine(); bool mail = text.Contains("Mail"); if(!mail) { if ((text != "")) { int indexofTO = text.IndexOf("to"); int indexofSLOT = text.IndexOf("Slot"); if (indexofSLOT < indexofTO) //load { int indexofInfo = text.IndexOf("Info"); string dtTxt = text.Substring(0, (indexofInfo - 1)); DateTime dt = DateTime.Parse(dtTxt); string data = text.Substring(indexofInfo); string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1)); timeDate.Add(key, new DriveDT(dt, data)); } else //unload { string key = text.Substring((indexofTO + 2)); timeDate.Remove(key); } } } } while (!textIn.EndOfStream); }
modified on Monday, January 4, 2010 11:30 AM
Much better, thanks. Dictionary.Remove() removes an entry based on a key object, not the value. Even when two strings have equal content, they probably are different strings (unless they are simple reference copies, or both got interned). Not sure what the easy solution is, I seldom remove things from a Dictionary. [EDIT] No, that is not it. A simple experiment shows Dictionary.Remove uses the default Comparer, which for strings compares the content. Are you sure
indexofTO + 2
is correct? How about indexofTO + 3? [/EDIT] :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
modified on Monday, January 4, 2010 11:55 AM
-
I have a dictionary call timeDate. where DriveDT is an object. static Dictionary<string, DriveDT> timeDate = new Dictionary<string, DriveDT>(); I want to remove the value from the dictionary, the line im using is in the section else //unload But the line isnt working. timeDate.Remove(key);
private static void extractSE(string filePath) { StreamReader textIn = new StreamReader(filePath); do { string text = textIn.ReadLine(); bool mail = text.Contains("Mail"); if(!mail) { if ((text != "")) { int indexofTO = text.IndexOf("to"); int indexofSLOT = text.IndexOf("Slot"); if (indexofSLOT < indexofTO) //load { int indexofInfo = text.IndexOf("Info"); string dtTxt = text.Substring(0, (indexofInfo - 1)); DateTime dt = DateTime.Parse(dtTxt); string data = text.Substring(indexofInfo); string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1)); timeDate.Add(key, new DriveDT(dt, data)); } else //unload { string key = text.Substring((indexofTO + 2)); timeDate.Remove(key); } } } } while (!textIn.EndOfStream); }
modified on Monday, January 4, 2010 11:30 AM
Have you run this through the debugger? I bet the
key
is an empty string (or worse). If it can't find the key to remove, what exactly do you expectRemove()
to do?.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Have you run this through the debugger? I bet the
key
is an empty string (or worse). If it can't find the key to remove, what exactly do you expectRemove()
to do?.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001This line move media into a drive. 19/05/2009 10:53:40 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 So I store the date time and the slot number(key) of when the media was moved into a drive, and when I see a removed media line, I used the date time from when it was moved out to show how long the media has been in. I need to remove it from the dic because the media can be used again and I cant have duplicate keys. FYI, there can be load of media being used at once, which I why I need to store this information. An example of the data im reading in 19/05/2009 17:50:58 Info Moved cartridge from Library 2 Slot 4 to Tape Drive 1 19/05/2009 17:51:00 Info Moved cartridge from Library 2 Slot 8 to Tape Drive 2 19/05/2009 17:55:15 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 4 19/05/2009 17:56:20 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 19/05/2009 18:07:45 Info Moved cartridge from Library 2 Tape Drive 2 to Slot 8 19/05/2009 18:08:09 Info Moved cartridge from Library 2 Slot 7 to Tape Drive 2 20/05/2009 12:36:02 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 1 20/05/2009 12:36:07 Info Moved cartridge from Library 2 Slot 6 to Tape Drive 1 20/05/2009 18:23:19 Info Moved cartridge from Library 2 Tape Drive 2 to Slot 7
-
I have a dictionary call timeDate. where DriveDT is an object. static Dictionary<string, DriveDT> timeDate = new Dictionary<string, DriveDT>(); I want to remove the value from the dictionary, the line im using is in the section else //unload But the line isnt working. timeDate.Remove(key);
private static void extractSE(string filePath) { StreamReader textIn = new StreamReader(filePath); do { string text = textIn.ReadLine(); bool mail = text.Contains("Mail"); if(!mail) { if ((text != "")) { int indexofTO = text.IndexOf("to"); int indexofSLOT = text.IndexOf("Slot"); if (indexofSLOT < indexofTO) //load { int indexofInfo = text.IndexOf("Info"); string dtTxt = text.Substring(0, (indexofInfo - 1)); DateTime dt = DateTime.Parse(dtTxt); string data = text.Substring(indexofInfo); string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1)); timeDate.Add(key, new DriveDT(dt, data)); } else //unload { string key = text.Substring((indexofTO + 2)); timeDate.Remove(key); } } } } while (!textIn.EndOfStream); }
modified on Monday, January 4, 2010 11:30 AM
Well, one thing that stands out to me is that the way you construct your key value on the Add and Remove is not symmetric.
string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1));
string key = text.Substring((indexofTO + 2));
The key you use to Add has to match the key you use to Remove. -
Well, one thing that stands out to me is that the way you construct your key value on the Add and Remove is not symmetric.
string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1));
string key = text.Substring((indexofTO + 2));
The key you use to Add has to match the key you use to Remove.The reason for this is because the location of the key is different for loading and unloading. 19/05/2009 10:53:40 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 //Load 19/05/2009 11:28:05 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 1 //Unload
-
This line move media into a drive. 19/05/2009 10:53:40 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 So I store the date time and the slot number(key) of when the media was moved into a drive, and when I see a removed media line, I used the date time from when it was moved out to show how long the media has been in. I need to remove it from the dic because the media can be used again and I cant have duplicate keys. FYI, there can be load of media being used at once, which I why I need to store this information. An example of the data im reading in 19/05/2009 17:50:58 Info Moved cartridge from Library 2 Slot 4 to Tape Drive 1 19/05/2009 17:51:00 Info Moved cartridge from Library 2 Slot 8 to Tape Drive 2 19/05/2009 17:55:15 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 4 19/05/2009 17:56:20 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 19/05/2009 18:07:45 Info Moved cartridge from Library 2 Tape Drive 2 to Slot 8 19/05/2009 18:08:09 Info Moved cartridge from Library 2 Slot 7 to Tape Drive 2 20/05/2009 12:36:02 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 1 20/05/2009 12:36:07 Info Moved cartridge from Library 2 Slot 6 to Tape Drive 1 20/05/2009 18:23:19 Info Moved cartridge from Library 2 Tape Drive 2 to Slot 7
So do any of the keys match the string you're building when you do your index math? That was my point.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
So do any of the keys match the string you're building when you do your index math? That was my point.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001When I step through with the debugged. "Slot 1" is stored as the key in the dic and the on the loop that goes through to remove it the string key is also Slot 1.
-
The reason for this is because the location of the key is different for loading and unloading. 19/05/2009 10:53:40 Info Moved cartridge from Library 2 Slot 1 to Tape Drive 1 //Load 19/05/2009 11:28:05 Info Moved cartridge from Library 2 Tape Drive 1 to Slot 1 //Unload
I understand the reason for it. I don't think your implementation is right. I think that you are off by 1 on your index in the second case so you end up with the wrong key value. If I am right, you are including a leading space in the key. I am willing to be proved wrong, however, because that is just off the top of my head without trying it to check.
-
Well, one thing that stands out to me is that the way you construct your key value on the Add and Remove is not symmetric.
string key = text.Substring(indexofSLOT, (indexofTO - indexofSLOT - 1));
string key = text.Substring((indexofTO + 2));
The key you use to Add has to match the key you use to Remove.When you use
string key = text.Substring((indexofTO + 2));
the
Substring
method returns all the remaining text in the string starting from the supplied index. If you feel that the index position is correct try supplying a length parameter or verify there is no funky data at the end of the string. I've seen cases where odd characters have been thrown onto the end and caused me all kind of headaches especially when they are non-visual characters that most text editors won't render (like a Vertical Tab, ascii 11?). I recommend stopping at the above line in the debugger and checking all characters from indexofTO + 2 thru text.Length - 1 in the Watch window; check each individual character position and see if it contains the expected data.Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
-
When I step through with the debugged. "Slot 1" is stored as the key in the dic and the on the loop that goes through to remove it the string key is also Slot 1.
Paul Harsent wrote:
to remove it the string key is also Slot 1.
I don't think so, I expect it is " Slot 1" with a space. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
I understand the reason for it. I don't think your implementation is right. I think that you are off by 1 on your index in the second case so you end up with the wrong key value. If I am right, you are including a leading space in the key. I am willing to be proved wrong, however, because that is just off the top of my head without trying it to check.
I feel very stupid. I was checking for the following space and having looked at the code for so long didnt see the lead space. Thanks very much.