Classic ASP check if an element in array exist - arrays

I have the below code, i want to check if index 3 in array is existing or not, but i always got this error :
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '[number: 0]'
urlArray=Split(url1,"/")
If (not isNull(urlArray(3))) then
If (urlArray(3)="site") Then
newUrl=urlArray(0) &"/"& urlArray(1) &"/"& urlArray(2) &"/m/" & urlArray(4) & "/" & urlArray(5)
Else
newUrl= url1
End If
Else
newUrl= url1
End If

Use function "IsArray" to make sure that variable is array, see here:
IsArray returns True if the variable is an array; otherwise, it
returns False. IsArray is especially useful with variants containing
arrays.
Use function "UBound" to check upped bound of array, see here:
Returns the largest available subscript for the indicated dimension of
an array. UBound(arrayname[, dimension])
The lower bound for any dimension is always 0.

Related

Subscript out of range error (with array)

I have this code in VBS:
Dim Arg()
Set objArgs = WScript.Arguments
for i=0 to objArgs.Count
Arg(i) = Replace(objArgs(i),"\n",vbNewLine,1,-1,1)
... (yes, the for has a Next at the end)
(example arguments: "hello\nworld" "test" 0 64)
But when I run it, it throws an error: The subscript is out of range (line 4, column 3).
Am I incorrectly using the arrays, or is the problem in the for, or what is wrong?
Arrays in VBScript are zero ordinal based that means that if you have for example ten elements in the array they will be numbered zero to nine.
So when using Count it will return the number of elements not their position in the array so you will need to minus one from the Count or VBScript will report a "Subscript out of range" error as there is no 11th element in the array to iterate to.
As suggested in the comments, you want to do this;
For i = 0 To objArgs.Count - 1
The other issue is you declare a dynamic array (Dim Args()) but never initialise it before trying to insert elements into it. You could see this answer to a similar question that explains the problem. When using a dynamic array declaration you need to initialise it using ReDim.
Useful Links
subscript out of range while adding items to array
Visual Basic scripting dynamic array

How to iterate array in precondition?

I want to iterate through array in a precondition.
But It seems precondition part doesn't allow use of "from" and "across" syntax.
Is there a way to iterate through array in precondition?
insert_last (s: STRING)
require
new_is_longer_than_prevs:
-- here I want to iterate through array "arr" and if length of s is longer than all other previously stored string values in array
do
arr.force (s, arr.upper + 1)
end
The version suggested in the other reply works in most cases (it assumes the lower index of the array is 1). However, the across loop can be used directly on the array rather than on its index range:
new_is_longer_than_prevs:
across arr as c all s.count > c.item.count end
This version works for any lower index and is slightly more efficient at run-time.
You can use 'across ... as ... all ... end' or 'across ... as ... some ... end' in precondition and postcondition. The 'all' version is used to valid if a condition is True for every iteration and the 'some' version is used to valid if the condition is True for at least one iteration. You can use some thing like this in your code:
insert_last (s: STRING)
require
new_is_longer_than_prevs:
across arr.lower |..| arr.upper as la_index all s.count > arr[la_index.item].count end
do
arr.force (s, arr.upper + 1)
end

QTP - lbound() and ubound()

I'm having some troubles doing something easy: checking the most recent date in an array. I create an array of webelements. There are some dates in this array in "fixed" places and I want to take the most recent of them.
This is what I'm doing:
Set cc = Description.Create
cc("micclass").value="WebElement"
cc("name").value="arrow_down"
Set collcc=Browser("Br").Page("Page").ChildObjects(cc)
For i=lbound(collcc) to ubound(collcc)
Msgbox collcc(x).getroproperty("innertext")
x =x +9
Next
The problem is that the script stops at the beginning of the for, saying that there is a "wrong number of arguments or invalid property assignment ubound" (and the same happens with lbound.
What am I doing wrong?!
Just from memory, but i think ChildObjects does not return an array. Try with
for i = 0 to collcc.Count - 1
....
next
Child object is the collection of objects so you needs to loop through "for each " Snippet given below
for each col in collcc
Msgbox col.getroproperty("innertext")
Next
Thanks
Sai

check index value of multi-value parameter

I have a multi-value parameter how I check in iif condition that value of idex is null or not
now, I am using this expression
IIF(IsNothing(Parameters!FR.Value(0))," ","Test")
My parameter is hidden and some thing it has 4 values or some time less than 4.
when I check that values of index 3 value(3) and there is no value in this index. I shows error. How I check the index value of parameter.
The first problem is that there is no object at index 3, not that the value of the object is Nothing, so you will get an error trying to access the Value property of an object that doesn't exist.
The second problem is that IIF is a function not a language construct, so all parameters are evaluated before the function is called regardless of the value of the boolean condition. I can't see a way of trying to access the Value(3) property without the IIF function blowing up in your face when it doesn't exist. You could try something convoluted like this:
=IIF(Parameters!FR.Count >= 4, Parameters!FR.Value(IIF(Parameters!FR.Count >= 4, 3, 0)), "")
but that is still going to blow up in your face when there are no parameter values selected at all.
The easiest and safest way to do it would be to create a custom code function to which you pass the multi-value parameter and the index you want:
Function Get_FR_by_Index(ByVal parameter As Parameter, ByVal index As Integer) AS String
Dim Result As String
Result = ""
If parameter.IsMultiValue Then
If parameter.Count >= index+1 Then
Result = CStr(parameter.Value(index))
End If
End If
Return Result
End Function
then use this function to access the result:
=Code.Get_FR_by_Index(Parameters!FR, 3)
Remember that the index is zero-based, so the first Value is index 0.

Initialize Array to Blank custom type OCAML

ive set up a custom data type
type vector = {a:float;b:float};
and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.
the following
let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}
makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible?
You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program caused by uninitialized values.
If the values you eventually want to place in your array are not available yet, maybe you are creating the array too early? Consider using Array.init to create it at the exact moment the necessary inputs are available, without having to create it earlier and leaving it temporarily uninitialized.
The function Array.init takes in argument a function that it uses to compute the initial value of each cell.
How can you have nothing? When you retrieve an element of the newly-initialized array, you must get something, right? What do you expect to get?
If you want to be able to express the ability of a value to be either invalid or some value of some type, then you could use the option type, whose values are either None, or Some value:
let vecarr : vector option array = Array.create !max_seq_length None
match vecarr.(42) with
None -> doSomething
| Some x -> doSomethingElse
You can initialize and 'a array by using an empty array, i.e., [||]. Executing:
let a = [||];;
evaluates to:
val a : 'a array = [||]
which you can then append to. It has length 0, so you can't set anything, but for academic purposes, this can be helpful.

Resources