473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populate dictionary with text file VB

GTXY20
29 New Member
Hi All,

I have been able to create a successful application with Python but I am being asked to create in VB.

I have a text file with the following data:

1,a
1,b
1,c
2,a
2,a
2,b
3,a
3,c

I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

In Python I would have:

Expand|Select|Wrap|Line Numbers
  1. {1:(a,b,c), 2:(a,b), 3:(a,c)}
I am starting with the following in VB:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
  2.         Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
  3.         Dim openimp As New OpenFileDialog()
  4.         If openimp.ShowDialog() = DialogResult.OK Then
  5.             Dim sr As New System.IO.StreamReader(openimp.FileName)
  6.             sr.Close()
  7.         End If
  8.     End Sub
  9.  
How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

Any help and guidance wher to start is very much appreciated.

Thanks again.
Oct 17 '07 #1
3 3320
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Hi All,

I have been able to create a successful application with Python but I am being asked to create in VB.

I have a text file with the following data:

1,a
1,b
1,c
2,a
2,a
2,b
3,a
3,c

I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

In Python I would have:

Expand|Select|Wrap|Line Numbers
  1. {1:(a,b,c), 2:(a,b), 3:(a,c)}
I am starting with the following in VB:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
  2.         Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
  3.         Dim openimp As New OpenFileDialog()
  4.         If openimp.ShowDialog() = DialogResult.OK Then
  5.             Dim sr As New System.IO.StreamReader(openimp.FileName)
  6.             sr.Close()
  7.         End If
  8.     End Sub
  9.  
How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

Any help and guidance wher to start is very much appreciated.

Thanks again.
Are you having issues with reading the text file and extracting its values or with adding elements into the dictionary? Or all of it?
Dictionary has an Add method

Expand|Select|Wrap|Line Numbers
  1. int x = 0;//put your number here
  2. string val = "value";//
  3. uhdict.Add(x,val);
(Do apply the conversion to vb .net from one of the online converters)
Oct 17 '07 #2
GTXY20
29 New Member
Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

1,a
1,b
1.c


Key = 1

Value = a,b,c

So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

Thanks.
Oct 17 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

1,a
1,b
1.c


Key = 1

Value = a,b,c

So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

Thanks.
Hi ,
PLease have a look at some tutorials on how to use a Dictionary.
By the way you speak, I dont think you have looked at this datattype.

good luck
Oct 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
8111
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML. -------------------- ---------- ---------- ------- ------- 21/03/2005-04:06:03 XX,XX XX,XX 171 3,42 21/03/2005-12:23:53 XX,XX ...
1
1669
by: t0M | last post by:
It's nearly impossible to find anything on this because of the Dictionary class, included within the dotnet framework, that pollutes any search results pertinent to my question. I want to be able to access an array of words, just like it were a physical dictionary. It's for a stupid little project, but it involves decryption. Is there a...
2
1535
by: Berimor | last post by:
Hey guys, has anybody scripted online translators? I got the task to build one but it should traslate big pieces of text at once - up to 5000 words!!! Traslation is simple - just words replacing. How to create dictionary correct - DB based or file with plain text? I'm searching the way to gain as much speed as possible. I realise - (not...
1
6495
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp...
0
4043
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp...
9
3753
by: oz | last post by:
Hi All, I want to make a dictionary with windows application. My data is html format. therefore, i use webBrowser control on my windows form. If the user click any word in webBrowser control, word mean must browse to user (like babylon). I have a windows form. My form has webBrowser control. I fill a html content with javascript...
2
3125
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract(); StockContract.Dictionary = ContractDictionary<string, string>(); GridView1.DataSource=StockContract.Dictionary; So far so good. Now I assign something to...
20
10336
by: Simon Strobl | last post by:
Hello, I tried to load a 6.8G large dictionary on a server that has 128G of memory. I got a memory error. I used Python 2.5.2. How can I load my data? SImon
4
1550
by: John Townsend | last post by:
Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions. I have two text files. Each file contains data like this: Test file 1234 4567 8975 I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.