Sign in to follow this  
Followers 0
DanielTyrkiel

For $oElement In $oElements: help me understand

5 posts in this topic

Hi,

I'm trying to understand how does this work:

; *******************************************************
; Example 1 - Open a browser with the basic example, get the collection
; of all elements and display the tagname and innerText of each
; *******************************************************

#include <IE.au3>

Local $oIE = _IE_Example("basic")
Local $oElements = _IETagNameAllGetCollection($oIE)
For $oElement In $oElements
MsgBox(0, "Element Info", "Tagname: " & $oElement.tagname & @CR & "innerText: " & $oElement.innerText)
Next

in the "For" loop there is a variable "$oElement" but I can't see it defined anywhere.

I don't understand how it works.

I have searched the web, this forum, autoit help file and even the IE UDF, but can't find a clear answer.

Sorry if it's an easy one, but I can't use it if I don't understand it.

regards

Daniel

Share this post


Link to post
Share on other sites



Did you look in the help file for the For...In function?

The Variable will be created automatically with a Local scope, even when MustDeclareVars is on.

If the expression is an Object collection with no elements, or an multi-dimensional array, the loop will be skipped and the Variable will contain an empty string.

If the expression is not an Object nor an Array, the script stops with an error, unless a COM Error handler had been configured.

AutoIt Arrays are read-only when using For...In. While you can assign the variable inside the For...In loop a value, this change is not reflected in the array itself. To modify the contents of an array during enumeration, use a For...To loop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I've just read this: The Variable will be created automatically with a Local scope, even when MustDeclareVars is on.

So it's declared by the loop statement itself...

The statement operates on colections or arrays so it must recognise them somehow.

I think I'm getting it.

_IETagNameAllGetCollection retrieves a collection of sorts and the variable defined between "for" and "in" is used to pass

elements of the colection one by one.

Have I got it? :D

Share this post


Link to post
Share on other sites

Yes, that's it in a nutshell. If you have an array, or in your case a collection, it will loop through the elements. The variable name will contain the contents of each one of the elements of the array/object collection as it loops through it. So it would work like this.

Global $Array[3] = ["Boy", "Dog", "Cat"]
For $Elements In $Array
    ConsoleWrite("$Elements = " & $Elements & @CRLF)
Next

If you run that in SciTE you'll see what the variable $Elements contains each time through the loop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Cool, I ran it. Got it now. I didn't realise that this function assigned its own variable on the fly.

I've been learning to read the descriptions of functions a bit closer and noticing the fine differences.

Many thanks!

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0