Sign in to follow this  
Followers 0
shamanu

for loops and variables

4 posts in this topic

#1 ·  Posted (edited)

Hello,

I'm trying to get some information from a structured webpage but as a C programmer I'm a bit confused about the for statement and the variable handling.

My current code:

$oIE=_IECreate("www.page.com")
$sHTML = _IEDocReadHTML ($oIE)
;search title
For $sHTML In $sHTML
    If(StringInStr($sHTML, "name")>0)Then
        $sName=$sHTML
    EndIf
Next

AutoIt should search for the next line which has a "name" string in it and save this line then to $sName.

1. How can I get only the current line out of $sHTML in the for loop?

2. How can I jump 2 lines forward in the for loop because I need the information from there and don't wan't the for loop to handle the stuff between.

Edited by shamanu

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well, _IEDocReadHTML doesn't return an array, so For/In doesn't have anything to cycle through.

I'm guessing you want to read 1 line at a time? Maybe try $sHTML = StringSplit(_IEDocReadHTML($oIE), @LF).

Also a potential problem: For $sHTML In $sHTML. The first $sHTML might overwrite the actual value in the second $sHTML. Use a diff $var. Ex: For $var In $sHTML

Lastly, If(StringInStr($sHTML, "name")>0)Then looks like it may be comparing a string to a number... number being 0, so maybe checking if the string is true or false? Plus, spaces look kind of off. Try If StringInStr($sHTML, "name") > 0 Then.

edit - Anyway, you'll probably want something like this...

#include <inet.au3>

$oIE = 'http://worldofwarcraft.com/realmstatus/status.xml'
$sHTML = StringSplit(_INetGetSource($oIE), @LF)

For $sVar In $sHTML
    If StringInStr($sVar, 'Light') Then
        $sNewValue = $sVar
        ConsoleWrite(@LF & $sNewValue)
    EndIf
Next

ConsoleWrite(@LF & @LF)

Run the above in the Autoit version of SciTE. It looks at World of Warcraft's server status xml page, makes an array separated at each new line, and searches the array for entries with the word "Light." (Sorry for the late edit, but I had problems getting back on the site.)

Edited by xcal

Share this post


Link to post
Share on other sites

Yes, thanks a lot for the help. This works so far. The only problem left is how can I get the current index from the for loop and manipulate or use it?

Example code from my html file:

field[0]["name"] = "green";
field[0]["title"] = "everything what you want";
field[0]["id"] = "25";

Everytime when the string ["name"] appears I create a directory with the name (green) which works so far.

But after the directory is created I need the information from the next two lines in the same loop. Isn't there something like a index to access the next lines directly?

Share this post


Link to post
Share on other sites

I could solve the problem.

With UBound($sHTML) I can get the length of the array and manipulate the lines directly. I'm using now a For $sVar=0 To UBound($sHTML)-1 loop

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