Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dictionary object question 1

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
0
0
GB
Hi,

I have a text file with two items per line. E.g.

Code:
Monday=True
Tuesday=True
Wednesday=False

I need to be able to reference the day and it's corresponding boolean value.

I have a function with an array to split this out. Due to lack of skill, I am trying to use a dictionary object to return outside of the function.

This function reads the file and populates the dictionary object.

Code:
Function readCronTab(sFilePath)

Const ForReading = 1
Set oFile = fso.opentextfile(sFilePath,ForReading)

Dim strString, i, el
Dim arrArray(), arrTMP

' set init values
i = 0

Do Until oFile.AtEndOfStream

strNextLine = oFile.Readline
	
	If InStr(strNextLine,"#") = False Then
		arrTMP = Split(strNextLine,"=")
	
		' arrange all elements into 2-dimensional array
		For el = 0 to Ubound(arrTMP) step 2
			'WScript.echo i
			ReDim Preserve arrArray(2,i)
			arrArray(1,i) = arrTMP(el)
			arrArray(2,i) = arrTMP(el+1)
			i = i + 1		' increase index counter	
			'WScript.Echo arrTMP(el)& "=" & arrTMP(el+1)
			oDict.add arrTMP(el) , arrTMP(el+1)
		Next
	
		' display arrARRAY contents:
	End If
Loop
Set readCronTab = oDict

oFile.Close

End Function

The following sub attemps to call the returned dictionary:

sub process:
Code:
Sub process()
sFilePath = "CronTab.txt"
Set myDict = readCronTab(sFilePath)
For Each sDay In myDict
	WScript.Echo sDay & " " & myDict.items(sDay)
Next
End Sub

This sub error with "Object not a collection: 'myDict.items'


I'd be very grateful if someone could point me in the write direction. I believe there is a better way to return the actual 2D array directly from the function without having to read it into a dictionary object, however my skill is not that great.

Many thanks
 
Try myDict.item(sDay), notice item not items.

If you would rather just return the array, have you tried:
readCronTab = arrArray
 
Thanks jges,

Both suggestions worked. I Looked back at some code i wrote a while ago using the same method and noticed there was no 's'. Grrrr!.

If I return just the array, how might I be able to pull out different items. I think it should be something like:

Code:
Sub process()
sFilePath = "CronTab.txt"
For Each sDay In readCronTab(sFilePath)
	WScript.Echo sDay(0,1) 
Next

End Sub


Thanks again.
 
What about this ?
For Each sDay In myDict.Keys
WScript.Echo sDay & " " & myDict.item(sDay)
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top