For developers who still have a project or two supporting Classic ASP, resources are dwindling on the internet. Newer and better technologies are far outpacing ASP scripting and Classic ASP is technically no longer supported by Microsoft. The end of this technology is rapidly closing.
Even so, for those supporting Classic ASP projects, I think hints and tips are still useful and apropos. I’d like to share a couple useful functions.
There may come a time when you have a list of name-value pairs. This is how JSON, for instance, stores information. This is also what you see in connection strings, where name-value pairs are defined with an equal sign and separated by semi-colons.
Since Classic ASP doesn’t normally come with a set of nifty tools for this kind of thing, programmers have to write these simple helper functions themselves.
Consider the list “test1=5;test2=7;test3=1;test4=17”. What if you wanted to know the value of the key test3 to get the value of 1?
The following function may then be useful:
function FindValueInList(listofItems,byval keyToFind,listSeparator,valueSeparator)
' finds a value for an item in a list, the list separated by listSeparator and
' the value from its item by a value separator. For example, in this string:
' findValue("test1=5;test2=7","test2",";","=") equals 7
itemOut = ""
allValues = split(listOfItems,listSeparator)
lastItem = ubound(allValues)
itemToFind = trim(ucase(keyToFind))
for counterItem = 0 to lastItem
singleLine = split(allValues(counterItem),valueSeparator)
if ubound(singleLine) > 0 then ' this is an item/value combination
if itemToFind = ucase(trim(singleLine(0))) then
itemOut = singleLine(1)
FindValueInList = itemOut
This function takes four arguments.
listOfItems: This string contains the text list of key/value pairs. The parameter value passed for our example would be “test1=5;test2=7;test3=1;test4=17”.
keyToFind: This string contains the key name; for our example this would be “test3”.
listSeparator: This string contains the character which separates key/value pairs; for our example the value would be “;” (a semi-colon).
valueSeparator: This string contains the key/value separator. In this case, the equal sign is used.
Therefore, to call this function one would type:
valueToFind = FindValueInList("test1=5;test2=7;test3=1;test4=17",”test3”,”;”,”=”)
valueToFind would be set to 1.
If you took this one step further, you could create the following function:
Where dbCon was an ADO database connection object. You could search for a value, such as a database server name using this function. With IIS 5.5, you might even be able to pull the password.
As you can see, this is a pretty simple function. It cannot handle nested key-value pairs and JSON might give it a problem as it is written. But this function can be a great starting point for more complicated tasks.