vbscript - VB Script (VBS) array reference -
i have 2 arrays
array1 = array("elem1", "elem2", "elem3") array2 = array("item1", "item2", "item3")
i select 1 of arrays
randomize dim refarray if rnd < 0.5 refarray = array1 else refarray = array2 end if
and make changes elements
refarray(0) = "foo" refarray(1) = "bar"
say rnd less 0.5 , refarray = array1 executes. both array1 , refarray point same piece of memory, when make changes refarray should visible in array1.
after code executes expect contents of array1 be: "foo", "bar", "elem3". instead remains unchanged.
the problem having vbs not pass reference array1 or array2, instead duplicates new array refarray, gets changes , leaves array 1 , 2 unchanged.
how can reference array , have changes made refarray apply referenced object (normal java/c usage)?
thanks.
the way reference native vbscript array sub/function call:
>> sub assignarray(a, i, e) >> a(i) = e >> end sub >> array1 = array("elem1", "elem2", "elem3") >> array2 = array("item1", "item2", "item3") >> wscript.echo "array1", join(array1), "array2", join(array2) >> assignarray array1, 0, "abra" >> assignarray array2, 0, "cadabra" >> wscript.echo "array1", join(array1), "array2", join(array2) >> array1 elem1 elem2 elem3 array2 item1 item2 item3 array1 abra elem2 elem3 array2 cadabra item2 item3
if not solve real-world problem - btw: is real-word problem? - consider use objects (dictionary, system.collections.arraylist) instead.
to spell out:
array-assignment copies. references (native) arrays possible parameter passing only. vbscript neither c nor java, you'll have adapt 'design' language - e.g.:
option explicit sub assignarray(a, i, e) a(i) = e end sub randomize dim a1 : a1 = split("i don't believe this") dim a2 : a2 = split("solves real-word problem") wscript.echo "a1:", join(a1) wscript.echo "a2:", join(a2) if rnd < 0.5 assignarray a1, 0, "we" else assignarray a2, 3, "problems" end if wscript.echo "a1:", join(a1) wscript.echo "a2:", join(a2)
output:
a1: don't believe a2: solves real-word problem a1: don't believe a2: solves real-word problem a1: don't believe a2: solves real-word problem a1: don't believe a2: solves real-word problems
Comments
Post a Comment