Thanks:  0
Likes:  0
Dislikes:  0

1. QTP: Compare two-dimensional Array

Hi,

I need to Compare two-dimensional Array and print the error message if there is difference.

sArray1(3,2)
sArray2(3,2)

' First Array
sArray1(0,0)="India,Gujarat"
sArray1(0,1)="India,Punjab"
sArray1(0,2)="US,New Jersey"

' Second Array
sArray2(0,0)="India,Gujarat"
sArray2(0,1)="India,Maharashtra"
sArray2(0,2)="US,New Jersey"

I need to show the difference under Report event log.

Thanks,
Prashant Patel

2. Re: QTP: Compare two-dimensional Array

Prashant

Look at this below - Use Reporter.reportevent instead of a msgbox

sArray1(3, 2)
sArray2(3, 2)

' First Array
sArray1(0, 0) = "India,Gujarat"
sArray1(0, 1) = "India,Punjab"
sArray1(0, 2) = "US,New Jersey"
' Second Array
sArray2(0, 0) = "India,Gujarat"
sArray2(0, 1) = "India,Maharashtra"
sArray2(0, 2) = "US,New Jersey"

For i = LBound(sArray1, 1) To UBound(sArray1, 1)
For j = LBound(sArray1, 2) To UBound(sArray1, 2)
If sArray1(i, j) &lt;&gt; sArray2(i, j) Then

MsgBox sArray1(i, j) & " &lt;&gt; " & sArray2(i, j), vbCritical, "NOT EQUAL"
Else
MsgBox sArray1(i, j) & " == " & sArray2(i, j), vbCritical, "EQUAL"

End If
Next
Next

3. Re: QTP: Compare two-dimensional Array

Thank you BrainZ. I just made small change. I was getting error message in the if else. So I trim the array and did not get an error.

sArray1(3, 2)
sArray2(3, 2)

' First Array
sArray1(0, 0) = "India,Gujarat"
sArray1(0, 1) = "India,Punjab"
sArray1(0, 2) = "US,New Jersey"
' Second Array
sArray2(0, 0) = "India,Gujarat"
sArray2(0, 1) = "India,Maharashtra"
sArray2(0, 2) = "US,New Jersey"

For i = LBound(sArray1, 1) To UBound(sArray1, 1)
For j = LBound(sArray1, 2) To UBound(sArray1, 2)
If trim(sArray1(i, j)) &lt;&gt; trim(sArray2(i, j)) Then

MsgBox sArray1(i, j) & " &lt;&gt; " & sArray2(i, j), vbCritical, "NOT EQUAL"
Else
MsgBox sArray1(i, j) & " == " & sArray2(i, j), vbCritical, "EQUAL"

End If
Next
Next

Prashant Patel

[ 07-21-2005, 06:05 AM: Message edited by: ppat7046 ]

4. Re: QTP: Compare two-dimensional Array

I get an error message stating "Subscript out of range: '4'". I printed the value, but both array have no value.

How do I solve it?

Thanks,
Prashant Patel

5. Re: QTP: Compare two-dimensional Array

ppat7046
try this out:

dim sArray1(3, 2)
dim sArray2(3, 2)

' First Array
sArray1(0, 0) = "India,Gujarat"
sArray1(0, 1) = "India,Punjab"
sArray1(0, 2) = "US,New Jersey"
' Second Array
sArray2(0, 0) = "India,Gujarat"
sArray2(0, 1) = "India,Maharashtra"
sArray2(0, 2) = "US,New Jersey"

For i =0 to 3
For j = 0 to 2
If sArray1(i, j) &lt;&gt; sArray2(i, j) Then

MsgBox sArray1(i, j) & " &lt;&gt; " & sArray2(i, j), vbCritical, "NOT EQUAL"
Else
MsgBox sArray1(i, j) & " == " & sArray2(i, j), vbCritical, "EQUAL"

End If
Next
Next

6. Re: QTP: Compare two-dimensional Array

As of March 3rd, 2006, this code is working fine to compare TWO dimensional array.

</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">dim ArrayName1(3,2)
dim ArrayName2(3,2)

' First Array
ArrayName1(0,0)=&quot;India,Gujarat&quot;
ArrayName1(0,1)=&quot;India,Punjab&quot;
ArrayName1(0,2)=&quot;US,New Jersey&quot;

' Second Array
ArrayName2(0,0)=&quot;India,Gujarat&quot;
ArrayName2(0,1)=&quot;India,Maharashtra&quot;
ArrayName2(0,2)=&quot;US,New Jersey&quot;</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica"></font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">If ubound(ArrayName1,1) &lt;&gt; ubound(ArrayName2,1) Then
Reporter.ReportEvent micFail,&quot;Number of ROWS not equal&quot;, ubound(ArrayName1,1) &amp; &quot;&lt;&gt;&quot; &amp; ubound(ArrayName2,1)
exitrun(0)
End If

If ubound(ArrayName1,2) &lt;&gt; ubound(ArrayName2,2) Then
Reporter.ReportEvent micFail,&quot;Number of COLUMNS not equal:&quot;,ubound(ArrayName1,2) &amp; &quot;&lt;&gt;&quot; &amp; ubound(ArrayName2,2)
exitrun(0)
End If

for i = 0 to ubound(ArrayName1,1) - 2
for j = 0 to ubound(ArrayName1,2) - 1
If ArrayName1(i,j) = ArrayName2(i,j) then
Reporter.ReportEvent micPass,&quot;Item Found&quot;, ArrayName1(i,j) &amp; &quot;=&quot; &amp; ArrayName2(i,j)
else
end if
next
next</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">

7. Re: QTP: Compare two-dimensional Array

Greetings, Prashant and et. al.. I enjoyed all's posts and perspectives! Prashant's code handled 2-dimension array comparison and exception handling in great details!! Those code may be re-used easily for those who need to so similar comparion. One note is that loop control may be updated to "for i = 0 to ubound(ArrayName1,1)" (and ubound(ArrayName2, 1) etc." to cover all allowed items. Comparing 2 dimension array has been a challenging task as some array features (UDT and optional base) were not included in VBScript. Prashant and et. al. (thanks) came up superb workarounds.

[ 03-05-2006, 11:23 AM: Message edited by: Eddy Teng ]

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.