<% Dim fso, readStream, jokeDict, totalJokes, key, jokeString, delim, setup, delivery ' ----------------------------------- ' Open a connection to the text file. ' 1 = ForReading ' ----------------------------------- Set fso = Server.CreateObject("Scripting.FileSystemObject") Set readStream = fso.OpenTextFile(Server.MapPath("knock.txt"), 1) ' -------------------------------------------------- ' Iterate through the file, reading each line into a ' Dictionary object with the line number as the key. ' -------------------------------------------------- Set jokeDict = CreateObject("Scripting.Dictionary") Do While readStream.AtEndOfStream = False jokeDict.Add readStream.Line, readStream.ReadLine totalJokes = totalJokes + 1 Loop ' ----------------------------------- ' Generate a random integer; the ' range is the total number of jokes. ' ----------------------------------- Randomize() key = Int(totalJokes * Rnd() + 1) ' --------------------------------- ' Split a randomly selected string. ' --------------------------------- jokeString = CStr(jokeDict.Item(key)) delim = CInt(InStr(jokeString, ":")) setup = Left(jokeString, delim-1) delivery = Mid(jokeString, delim+1) ' -------------------------------------- ' Close the connection to the text file. ' -------------------------------------- readStream.Close Set readStream = Nothing Set fso = Nothing %> <% ' %>
Knock knock! <%= setup %> <%= delivery %> Would you like to hear another? Thank you!