Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Removing values in a dictionary [modified]

Removing values in a dictionary [modified]

Scheduled Pinned Locked Moved C#
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Harsent
    wrote on last edited by
    #1

    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

    L OriginalGriffO realJSOPR D 5 Replies Last reply
    0
    • P Paul Harsent

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Paul Harsent wrote:

      the line isnt working

      that is unclear. PS: please use PRE tags (the "code block" widget), I'm not interested in unformatted code. You can still edit the message. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      1 Reply Last reply
      0
      • P Paul Harsent

        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

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L P 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          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.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            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.

            P Offline
            P Offline
            Paul Harsent
            wrote on last edited by
            #5

            Done, thanks. didnt realise.

            1 Reply Last reply
            0
            • P Paul Harsent

              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

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              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 expect Remove() 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

              P 1 Reply Last reply
              0
              • P Paul Harsent

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • realJSOPR realJSOP

                  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 expect Remove() 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

                  P Offline
                  P Offline
                  Paul Harsent
                  wrote on last edited by
                  #8

                  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

                  realJSOPR 1 Reply Last reply
                  0
                  • P Paul Harsent

                    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

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #9

                    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.

                    P B 2 Replies Last reply
                    0
                    • D David Skelly

                      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.

                      P Offline
                      P Offline
                      Paul Harsent
                      wrote on last edited by
                      #10

                      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

                      D 1 Reply Last reply
                      0
                      • P Paul Harsent

                        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

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #11

                        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

                        P 1 Reply Last reply
                        0
                        • P Paul Harsent

                          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

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #12

                          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.

                          P 1 Reply Last reply
                          0
                          • realJSOPR realJSOP

                            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

                            P Offline
                            P Offline
                            Paul Harsent
                            wrote on last edited by
                            #13

                            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.

                            L 1 Reply Last reply
                            0
                            • D David Skelly

                              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.

                              B Offline
                              B Offline
                              Ben Fair
                              wrote on last edited by
                              #14

                              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?

                              1 Reply Last reply
                              0
                              • P Paul Harsent

                                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.

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                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.


                                1 Reply Last reply
                                0
                                • D David Skelly

                                  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.

                                  P Offline
                                  P Offline
                                  Paul Harsent
                                  wrote on last edited by
                                  #16

                                  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.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups