SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    270
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Get index value from an array

    Hi All,
    i am searching for an particular element in an array with the help of Filter command.
    if it is there then my filtered array will be having ubound value as 0, every thing is fine until now...
    but i want to know the index of the original array where this element is present, how to get that...
    r else to put in simple words..i want to know the index of an array by giving one of the element in array.

    Cheers
    Yogi

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Location
    Northern Ireland
    Posts
    529
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Get index value from an array

    for i = lbound(arrayname) to ubound(arrayname)
    if arrayname(i) = "DesiredElement" then
    reporter.reportevent 0, "Desired Element", "The text was element " & i
    end if
    next
    I'm not a complete idiot - some parts are missing.

  3. #3
    Senior Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    270
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Get index value from an array

    Hi,
    Thankx MoTest.......but
    No, i dont want to use for loop....just in one step if i pass the element it should return me appropriate index of an array

    Thanks
    Yogi

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Location
    Northern Ireland
    Posts
    529
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Get index value from an array

    Can I ask why it is that you need a 1-line solution?

    The snippet of code I provided you will return you the appropriate index if you add the line 'Exit For' just after the reporter.reportevent line. The index will be i.

    You could always wrap the code above into a function and call it passing your array and desired element:

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">Public Function FindIndex(Array, Element)
    for i = lbound(Array) to ubound(Array)
    if Array(i) = Element then
    FindIndex = i
    exit function
    end if
    next
    FindIndex = false 'Element wasn't in array
    end function</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">So if you have the above code in a vbs file, and have it attached to your test, you can, in one line, find the index of a particular element by using the syntax:

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">myIndex = FindIndex(&quot;ArrayName&quot;, &quot;ElementToFind&quot
    msgbox myIndex 'Myindex will be either the element number, or False if the element did not exist.</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">Perhaps this will help you?
    I'm not a complete idiot - some parts are missing.

  5. #5
    Senior Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    270
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Get index value from an array

    Hi MoTest,
    Actually my scneario is select user passed value from weblist box, to acheive this priorly i was using for loop in which i compare the user given data with the list box, if found then with the help of select method i use to select an item, but if the list box is very large, then it was taking time, which was effecting the performance of the script...
    to avoid this, with the help of 'all items' method of list box, i collect all items in an array then with the help of filter command, i will come to know whether the user passed value is present in list box or not..if present then with the help of select method i used to select the item, but here problem is while selecting if user passes the value has Testing, but in list box the value is testing, where in T is in lower case and if i directly use select method, then run time error will accur...
    i hope u got my lengthy story.....

    Cheers
    Yogi

  6. #6
    Junior Member
    Join Date
    Aug 2005
    Location
    Bangalore
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Get index value from an array

    Yogi,
    After getting the required item using Filter function check whether retutned array contains the exact item. Because If the array you are filtering contains substring also it will return that element in the filtered array.

    So after filtering add this statement:

    if 'arrTest; is filtered array

    If(strcomp(arrTest(0),itemtobesearched,0)= 0)Then
    &lt;YOUR ITEM IS PRESENT&gt;
    Else
    &lt;YOUR ITEM IS NOT PRESENT&gt;
    End If

    Hope this solves your problem.

    Cheers
    Rohith Shetty

 

 

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.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 07:20 AM.

Copyright BetaSoft Inc.