2

Arrays tend to look like this in languages such as JavaScript and Python:

['Foo', 'Bar']

But in classic ASP they look like this:

Array("foo", "bar")

Is there an easy way to store a modern array in a VBScript array?

As an example of the problem this will give a type mismatch:

Dim arrayString
arrayString = "['Foo', 'Bar']"

Dim myArray()
myArray = array(arrayString)

If someone has built a library to do this conversion that would be most helpful, otherwise I'm guessing we need to do something cumbersome with split?

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
Mike Poole
  • 1,958
  • 5
  • 29
  • 41
  • Parse the string and build an array [using a dynamic array](https://stackoverflow.com/a/17664578/692942). There is no "out of the box" approach you have to build something. – user692942 Oct 18 '22 at 13:45
  • That's the approach I am building, it's surprising that there is no standard pattern for such a common requirement. No idea why someone voted down my question. If someone thinks the question is poor please have the courtesy to say why. – Mike Poole Oct 18 '22 at 14:33

2 Answers2

2

Are you aware that ASP classic also supports JScript (ECMAScript Version 3)? For instance, we can put function wrappers on JSON parsing and stringify functions and JScript arrays as follows:

<%@ Language= "Javascript" %> 
<% 
//json2-min.js
if(typeof JSON!=="object"){JSON={}}(function(){"use strict";function f(e){return e<10?"0"+e:e}function quote(e){escapable.lastIndex=0;return escapable.test(e)?'"'+e.replace(escapable,function(e){var t=meta[e];return typeof t==="string"?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'}function str(e,t){var n,r,i,s,o=gap,u,a=t[e];if(a&&typeof a==="object"&&typeof a.toJSON==="function"){a=a.toJSON(e)}if(typeof rep==="function"){a=rep.call(t,e,a)}switch(typeof a){case"string":return quote(a);case"number":return isFinite(a)?String(a):"null";case"boolean":case"null":return String(a);case"object":if(!a){return"null"}gap+=indent;u=[];if(Object.prototype.toString.apply(a)==="[object Array]"){s=a.length;for(n=0;n<s;n+=1){u[n]=str(n,a)||"null"}i=u.length===0?"[]":gap?"[\n"+gap+u.join(",\n"+gap)+"\n"+o+"]":"["+u.join(",")+"]";gap=o;return i}if(rep&&typeof rep==="object"){s=rep.length;for(n=0;n<s;n+=1){if(typeof rep[n]==="string"){r=rep[n];i=str(r,a);if(i){u.push(quote(r)+(gap?": ":":")+i)}}}}else{for(r in a){if(Object.prototype.hasOwnProperty.call(a,r)){i=str(r,a);if(i){u.push(quote(r)+(gap?": ":":")+i)}}}}i=u.length===0?"{}":gap?"{\n"+gap+u.join(",\n"+gap)+"\n"+o+"}":"{"+u.join(",")+"}";gap=o;return i}}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()}}var cx,escapable,gap,indent,meta,rep;if(typeof JSON.stringify!=="function"){escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;meta={"\b":"\\b","  ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};JSON.stringify=function(e,t,n){var r;gap="";indent="";if(typeof n==="number"){for(r=0;r<n;r+=1){indent+=" "}}else if(typeof n==="string"){indent=n}rep=t;if(t&&typeof t!=="function"&&(typeof t!=="object"||typeof t.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":e})}}if(typeof JSON.parse!=="function"){cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;JSON.parse=function(text,reviver){function walk(e,t){var n,r,i=e[t];if(i&&typeof i==="object"){for(n in i){if(Object.prototype.hasOwnProperty.call(i,n)){r=walk(i,n);if(r!==undefined){i[n]=r}else{delete i[n]}}}}return reviver.call(e,t,i)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(e){return"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})()
function jsonParse(str) { return JSON.parse(str); }
function jsonStringify(obj) { return JSON.stringify(obj); }
function jsArray() { return []; }
function jsArrayPush(a,v) { a.push(v); }
%>

Then, in your VBScript code you can make use of the JScript array, e.g.

<%@ Language= "VBscript" %> 
<% 
Dim arr, str
str = "[""Foo"", ""Bar""]"
Set arr = jsonParse(str)
Call jsArrayPush(arr, "Hello")
Call jsArrayPush(arr, "World")
str = jsonStringify(arr) ' ["Foo","Bar","Hello","World"]
%>
user692942
  • 16,398
  • 7
  • 76
  • 175
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
  • Actually, it supports any Active Scripting language of which JScript (ECMAScript v3) is one not JavaScript. Have amended the answer. – user692942 Oct 19 '22 at 05:38
  • 1
    Many thanks for that Stephen, that's exactly the type of solution that I was hoping was commonly used. I will store the `json2-min.js` helper functions in an include so I can resuse them. This approach is especially useful as in my actual problem I am dealing with a 2D array. I did know that JScript could be used in classic ASP but it had not dawned on me to use it in this case. Until your answer my best approach was to provide the array as a delimited string (I also control the Node app that provides the data). – Mike Poole Oct 19 '22 at 05:42
  • 1
    @MikePoole again (as already stated) it’s not JavaScript it’s JScript. If you try using modern JavaScript syntax you will be disappointed. – user692942 Oct 19 '22 at 05:45
  • 1
    Thanks for pointing out my typo, I corrected my comment. – Mike Poole Oct 19 '22 at 05:47
  • Makes sense to use JSON in the native environments that supports it naturally. It's rare to see ASP classic and VBscript in use these days. – Stephen Quan Oct 19 '22 at 07:24
  • It’s not as rare as you think. I constantly find myself supporting clients with existing corporate systems built with Classic ASP. – user692942 Oct 19 '22 at 07:48
0

You're comparing two different things. Your example of a "modern array" is what is called literal syntax. This tells the processor to create an array using the data and structure provided. The indexing order is typically preserved in these languages.

Your classic examples using Array() are array generating functions. They create arrays in memory with provided values, but don't necessarily imply any sort of structure at all. Many languages specifically indicate that value indexing can vary. Some languages process arrays internally as objects and often require the new keyword in addition to the array function. When this happens it's often referred to as constructor syntax.

Many languages offer one, two, or even all three ways of creating arrays.

VBScript does not offer any variation of the literal syntax.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
  • Thanks for your reply @nilpo . That is my understanding too. What is a common approach of parsing an array (which is simply a string) so it populates a vbscript array? Would I need to cobble something together using `split` or is there a well eatblished pattern. – Mike Poole Oct 18 '22 at 10:36
  • @MikePoole The most common way is to loop across the characters of the string using the `Mid` function. – Nilpo Oct 19 '22 at 00:32