?
Solved

Hashtable read text file???

Posted on 2004-08-23
6
Medium Priority
?
247 Views
Last Modified: 2012-06-27
I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help.........

123.123.123.123      www.mywebsite.com
111.222.333.444      www.nowebsite.com
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876438

    static Hashtable ReadFromFile(string filename)
    {
    StreamReader SR;
    string S;
    Hashtable ret = new Hashtable
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null)
    {
         string [] tmp = S.Split("\t");
         if(tmp.Count == 2) {
              if(!ret.Contains(tmp[0]) {
                    ret.add(tmp[0], tmp[1])
              } else {
                    how do you want to handle duplicate entries ?
              }
         } else {
              throw new system.exception("bad data");
         }
    S=SR.ReadLine();
    }
    SR.Close();
    }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876445
btw this will most likely require a little editing no compiler here this sec so just typed here.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11876915

  I like the coding. I have an error in my code that says:

"Index was outside the bounds of the array"

All I changed was SR = File.OpenText("MyFile.txt");  

????????????????
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
somnatic earned 2000 total points
ID: 11878436
i hope gregoryyoung won't kill me for editing his code ;)
try this .. works for me .. and .. don't forget the using System.IO; Statement



            private void button1_Click(object sender, System.EventArgs e)
            {
                  Hashtable r = ReadFromFile("d:\\testtab.txt");
            }
            static Hashtable ReadFromFile(string filename)
            {
                  StreamReader SR;
                  string S;
                  Hashtable ret = new Hashtable();
                  SR=File.OpenText(filename);
                  S=SR.ReadLine();
                  while(S!=null)
                  {
                        string [] tmp = S.Split('\t');
                        if(tmp.Length == 2)
                        {
                              if(!ret.Contains(tmp[0]) )
                              {
                                    ret.Add(tmp[0], tmp[1]);
                              }
                              else
                              {
                              }
                        }
                        else
                        {
                              throw new System.Exception("bad data");
                        }
                        S=SR.ReadLine();
                  }
                  SR.Close();

                  return ret;
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11888402
jeeze forget a return statement typing without a compiler and lose all credit ... nice one.
0
 
LVL 4

Expert Comment

by:somnatic
ID: 11889478
you forgot the semicolon somewhere at the beginning ;) ..

any way to shift points ?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question