+2 votes
Hello, there, Can you please help me?

I need to add new elements in an one dimension array during playback. Also, I need to check if the new element is in the array, if not then add it, if it has that new element, then it will not add to the array.

For example, during the script is running,

it found A, then it will match the Array [temp],  if A is not in the [temp], then it will add it to the Array [temp], if it is in the [temp], then it wont add it.

so two parts, match array part and add element part, both automatically during playback.

I think this is very challenging for QA wizard to do that.

But thanks in advance
asked Jul 8, 2016 in QA Wizard Pro by Ana

1 Answer

0 votes
Best answer

Hi Ana, it's been a while since you asked this so possibly you already have an answer. If you put the below code into an empty script you should be able to run it and play around with it to work out what it does and how. I've put in some comments to try to explain, but ask any questions if I haven't explained enough. You'll need to work out how to tie in this example with your existing code, but this should help give an idea of the direction to go.

ClearOutput()

arrArray = ParseString("A,B,C,D", ",")

Sub CheckElement(strElement)

	intArrayPosition = 0
	strElementInArray = "Element Not Found"

	'Loop through each element in the array to check if it matches the current element:
	Do
		intArrayPosition = intArrayPosition + 1
		
		if arrArray(intArrayPosition) = strElement then
			strElementInArray = "Element Found"
			'Change the position to the size of the array so can immediately exit the loop once the element has been found:
			intArrayPosition = ArraySize(arrArray, 1)		
		end if
		
	Loop Until intArrayPosition = ArraySize(arrArray, 1)
	
	If strElementInArray = "Element Not Found" then
		'If the element isn't found then:
		intNewElementArrayPosition = ArraySize(arrArray, 1) + 1
		Redim Preserve arrArray(intNewElementArrayPosition)			'Resize the array and use Preserve to keep the existing values
		'Add the new element to the array:
		arrArray(intNewElementArrayPosition) = strElement
		println(strElement + " added to the array")
	else
		println(strElement + " already in array")
	end if
	
End Sub

'Run some element checks and print results to Output:
CheckElement("A")

println("Array has " + ArraySize(arrArray, 1) + " elements")
println("")

CheckElement("B")
println("Array has " + ArraySize(arrArray, 1) + " elements")
println("")

CheckElement("C")

println("Array has " + ArraySize(arrArray, 1) + " elements")
println("")

CheckElement("D")

println("Array has " + ArraySize(arrArray, 1) + " elements")
println("")

CheckElement("E")

println("Array has " + ArraySize(arrArray, 1) + " elements")
println("")

println("Array element 1 is [" + arrArray(1) + "]")
println("Array element 2 is [" + arrArray(2) + "]")
println("Array element 3 is [" + arrArray(3) + "]")
println("Array element 4 is [" + arrArray(4) + "]")
println("Array element 5 is [" + arrArray(5) + "]")

 

answered Jul 22, 2016 by Carly (870 points)
selected Jul 22, 2016 by nico
248 questions
262 answers
106 comments
2,047 users