Results 1 to 7 of 7

Thread: How can we use nested For-Each in a function??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Contributing Member
    Join Date
    Feb 2007
    Answers
    44

    How can we use nested For-Each in a function??

    Hi,

    I want to use two "For Each" in a function.

    Eg; a=array("object1","object2","object3")
    b=array("a","b","c")
    For each i in a
    For each j in b
    Browser("asdas").Page("sdfd").webEdit(i).set j
    Exit for
    Next
    Next

    But wat is happening is, the First ForEach is not getting incremented but the second ForEach is incrementing. We want both i and j to increment simultaneously.

    Please let us know.

    Thanks,
    Sowmya


  2. #2
    Expert Member
    Join Date
    Oct 2007
    Answers
    375

    Re: How can we use nested For-Each in a function??

    Hi Sowmya,

    When you state -

    a=array("object1","object2","object3")
    b=array("a","b","c")
    For each i in a
    For each j in b
    Browser("asdas").Page("sdfd").webEdit(i).set j
    Next
    Next

    The logic of the above code woule be that it would pick up Object1 from array a and carry out 3 iterations for the same using all values of array b. Once 1st value is utilized from array a, it would increment to pick up second value from array a.

    NOTE : I have removed the Exit For statement.This enables the First loop to also increment which otherwise get Exited

    I really dont think we could have a condition where you wish to increment both the loop counters simultanesouly as it would negate the meaning of a nested loop.

    Cheers...


  3. #3
    Contributing Member
    Join Date
    Feb 2007
    Answers
    44

    Re: How can we use nested For-Each in a function??

    Thanks...

    But i want just one value for the object. Both should increment at the same time.

    Eg: a=array("editBox1","editBox2","editBox3")
    b=array("4","5","6")
    For Each i in a
    For Each j in b
    a=Browser("").Page("").webedit(i).Set j
    msgbox a
    Next
    Next

    Expected Output:editBox1= 4
    editBox2= 5
    editBox3= 6

    Actual Output : editBox1=4
    editBox1=5
    editBox1=6


    We want the expected result. how to do it.
    Plz help


  4. #4
    Expert Member
    Join Date
    Jan 2007
    Answers
    211

    Re: How can we use nested For-Each in a function??

    1. Nested Loops are used when there are multiple combinations.

    2. In your case there are One to One Mapping among the {Controls and Values} set.

    3. Only one Loop will do.

    4. Nested Loop E.G

    Dim AList(3) = {"Edit1", "Edit2", "Edit3"}
    Dim BVal(3) = {"Nawab", "Shaikh", "NawabShaikh"}

    For intCnt = 0 to UBound(AList)
    For intJCnt = 0 to UBound(BVal)
    Browser(br).Page(pg).Frame(fr).WebEdit(AList(intCnt)).Set BVal(intJCnt )

    '''Check for Some Test Condition
    Next
    Next




    Regards,
    Nawab


  5. #5
    Expert Member
    Join Date
    Oct 2007
    Answers
    375

    Re: How can we use nested For-Each in a function??

    Hi Sowmya,

    As Nawab has stated, your request can be completed using a single loop. But the catch here is that you must know the size of the second array and element index of second array being assigned to first array would always remain the same.

    a=array("editBox1","editBox2","editBox3")
    b=array("4","5","6")

    For i = 0 to UBound(a)

    a(i) = b(i)

    Next

    For j=0 to UBound(a)
    msgbox a(j)
    Next

    This would assign the Expected Output.

    NOTE : I cannot work with dynamic sized arrays in the above code since I am not extracting the Ubound of array 2.

    Cheers.....


  6. #6
    Contributing Member
    Join Date
    Feb 2008
    Answers
    36

    Re: How can we use nested For-Each in a function??

    Hi there,

    Your can use this code:
    namelable is an array containing the names of edit fields and data contains the data to be inserted.
    So,
    abcd will be inserted in wbedit whose name property = name_lastlea2
    vcxbhg will be inserted in wbedit whose name property = lea3
    and so on....

    namelabel = Array ("name_lastlea2","lea3","lea8")
    data = Array ("abcd","vcxbhg","99898456")

    For i=0 to ubound(namelabel)
    Browser("name:=abcd").Page("title:=abcd").WebEdit("name:=" & namelabel(i)).Set data(i)
    Next


    Regards,
    Tina


  7. #7
    Junior Member
    Join Date
    Mar 2008
    Answers
    4

    Re: How can we use nested For-Each in a function??

    'Do you really need For Each .. Next loop?
    'If no need we can slove using Single For .. Next loop

    a=array("editBox1","editBox2","editBox3")
    b=array("4","5","6")
    aCnt = UBound(a)
    For i = 0 to aCnt
    Val=Browser("").Page("").webedit(a(i)).Set b(i)
    msgbox Val
    Next

    Expected Output:
    editBox1= 4
    editBox2= 5
    editBox3= 6

    Actual Output :
    editBox1= 4
    editBox2= 5
    editBox3= 6


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact