-
May 19th, 2017, 02:43 PM
#1
Thread Starter
PowerPoster
add + 1 in dictionary(FSO object)
i loop a text file with the simple freefile.
during the loop i get from each line with mid statement a part of string and assign to MyVar.
Myvar is string dimensioned.
In this case myVar is the name of a City
now i need to assign on a City +1 value in a dictinary in loop...
example for pseudo code:
City="roma"
Not exists, in Dictionay record, for first occurrence Roma+1
next line from txt
City="Roma"
Roma alredy exist with 1 value
Roma,1+1
ecc...
In effect i need to count each occurence based the City to the end of txt
Then, now i need to loop each items in dictionary....
sory for my bad english
Last edited by luca90; May 19th, 2017 at 02:49 PM.
-
May 19th, 2017, 02:46 PM
#2
Re: add + 1 in dictionary
Dictionary allows for lookup by key so no need to loop through all entries.
If you were going to loop then a list would work fine.
That said I don't really see a question there so not sure what you are trying to ask.
-
May 19th, 2017, 02:53 PM
#3
Thread Starter
PowerPoster
Re: add + 1 in dictionary
Originally Posted by DataMiser
Dictionary allows for lookup by key so no need to loop through all entries.
If you were going to loop then a list would work fine.
That said I don't really see a question there so not sure what you are trying to ask.
other idea to count the occurence of City during the loop in txt file?
Simple txt:
....
Roma
Firenze
Firenze
Roma
Roma
Firenze
Firenze
Roma
Firenze
Firenze
Firenze
...
to the end of file, i need
Roma,4
Firenze, 8
ecc
Last edited by luca90; May 19th, 2017 at 02:58 PM.
-
May 20th, 2017, 12:40 PM
#4
Re: add + 1 in dictionary(FSO object)
Code:
oDict.CompareMode = vbTextCompare
...
If oDict.Exists(City) Then
oDict(City) = oDict(City) + 1
Else
oDict.add City, 1
End If
-
May 20th, 2017, 02:34 PM
#5
Thread Starter
PowerPoster
Re: add + 1 in dictionary(FSO object)
Originally Posted by Dragokas
Code:
oDict.CompareMode = vbTextCompare
...
If oDict.Exists(City) Then
oDict(City) = oDict(City) + 1
Else
oDict.add City, 1
End If
WORK FINE!
but how to loop the items in filled items of dictionary, to the and of txt?
-
May 20th, 2017, 06:53 PM
#6
Re: add + 1 in dictionary(FSO object)
but how to loop the items in filled items of dictionary,
no need, that is the point to using a dictionary, just put that code snippet into your loop to read the lines of file
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 21st, 2017, 05:44 AM
#7
Re: add + 1 in dictionary(FSO object)
As far as I understand, OP is using Dictionary just for summarizing data to get final log.
Code:
Dim key
...
for each key in oDict
debug.? key, oDict(key)
next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|