Pages

Friday, March 30

Find distinct unique values from array using QTP/vbscript

Hello Friends,

In this post, we will see, how to get unique/distinct values from an array.
We will do that using very useful and optimum feature of QTP - Dictionary Object.

Here is the code--

Dim objDict, oldArray, val
oldArray = Array ("11", "21", "11", "31", "21", "41", "31")

Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = vbTextCompare

For each val in oldArray
objDict(val) = val
Next

newArray = objDict.Items

For i=0 to ubound(newArray)
msgbox newArray(i)
Next

...n done!!!

Plz do post your queries/comments...

20 comments:

  1. Nice Code. It is reaching our expectations without any updates

    ReplyDelete
  2. Hi,

    Need small change in below part of above mentioned code...

    Incorrect One :
    For i=0 to ubound(newArray)
    msgbox newArray(i)
    Next

    Correct One :
    For i=0 to ubound(newArray)-1
    msgbox newArray(i)
    Next

    ReplyDelete
  3. objDict.CompareMode = vbTextCompare

    what did this code ?

    ll it run without this?

    ReplyDelete
  4. If you do without the Comparemode = vbTextCompare then the will be VbBinaryCompare. so any time if objDict found "11" & "11" binary values are different it will take as new value and you will get "11" 2 times.

    So to avoid that any point of time tell obDict to compare text wise.

    ReplyDelete
  5. How can you get items that only appear once. Meaning "41" only in the above mentioned array?

    ReplyDelete
  6. @Deepak, you're wrong Ubound returns the highest index of an array. So the code above is correct.

    ReplyDelete
  7. For each val in oldArray
    objDict(val) = val
    Next

    What the above code is doing?

    ReplyDelete
  8. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  9. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Selenium Training in Bangalore | Selenium Training in Bangalore | Selenium Training in Bangalore | Selenium Training in Bangalore

    ReplyDelete
  10. Great job.... Awesome list, just starting a blog and this is going to be a massive help. Thank you!

    Node JS Training in Chennai
    Node JS Training

    ReplyDelete
  11. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…

    Java training in Chennai | Java training in Bangalore

    Java online training | Java training in Pune

    ReplyDelete
  12. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    advanced excel training in bangalore | Devops Training in Chennai

    ReplyDelete
  13. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    python course in pune | python course in chennai | python course in Bangalore

    ReplyDelete


  14. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.


    AWS Training in BTM Layout |Best AWS Training in BTM Layout

    AWS Training in Marathahalli | Best AWS Training in Marathahalli

    ReplyDelete
  15. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete