Sign in to follow this  
Followers 0
olirav

Variable inside Variable name

10 posts in this topic

Is it possible to have a variable as part of another variables name?

I have a loop that is reading a html file line by line and what I want to have is a new var created for each line.

Just have a look:

$html = FileOpen("FILE", 0)
$CurrentLineNo = 0
If $html = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    ;Current Line Number increase
    $CurrentLineNo = $CurrentLineNo + 1
    $temp = FileReadLine($html)
    If @error = -1 Then 
        $CurrentLineNo = $CurrentLineNo - 1
        ExitLoop
    EndIf
    $TempTest = StringLeft($temp, 11)
    If $TempTest == "<DT><A HREF" Then
        $line&$CurrentLineNo = $temp
    Else
        $CurrentLineNo = $CurrentLineNo - 1
    EndIf
Wend

So the $line&$CurrentLineNo would be called $line1 for the first one

Share this post


Link to post
Share on other sites



Assign Could be what your looking for.

Assigns a variable by name with the data.

Assign ( "varname", "data" [, flag] )

Parameters

'varname' The name of the variable you wish to assign. Cannot be an array element.

'data' The data you wish to assign to the variable.

'flag' [optional] controls the way that variables are assigned (add required options together):

0 = (default) Create variable if required

1 = Force creation in local scope

2 = Force creation in global scope

4 = Fail if variable does not already exist


GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites

Assign Could be what your looking for.

The "Cannot be an array element." kills that I think, trying anything involving a variable in the name just throws up Assign^ ERROR

Oli

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

From a quick look on your code, better stick to an array. Try something like this (untested):

#include <array.au3>
$hFile = FileOpen("FILE", 0)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
Dim $aLines[1] = [0]
While 1
    $sFileLine = FileReadLine($hFile)
    If @error = -1 Then
        FileClose($hFile)
        ExitLoop
    endif
    If StringLeft($sFileLine, 11) = "<DT><A HREF" Then
        _ArrayAdd($aLines, $sFileLine)
        $aLines[0] += 1
    EndIf
WEnd

_ArrayDisplay($aLines)
MsgBox(0, "", $aLines[0])
If $aLines[0] > 0 Then MsgBox(0, "", $aLines[1])
Edited by KaFu

Share this post


Link to post
Share on other sites

From a quick look on your code, better stick to an array. Try something like this (untested):

#include <array.au3>
$hFile = FileOpen("FILE", 0)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
Dim $aLines[1] = [0]
While 1
    $temp = FileReadLine($html)
    If @error = -1 Then ExitLoop
    If StringLeft($temp, 11) = "<DT><A HREF" Then
        _ArrayAdd($aLines, $temp)
        $aLines[0] += 1
    EndIf
WEnd

_ArrayDisplay($aLines)
MsgBox(0, "", $aLines[0])
If $aLines[0] > 0 Then MsgBox(0, "", $aLines[1])

_ArrayDisplay($aLines)
MsgBox(0, "", $aLines[0])
If $aLines[0] > 0 Then MsgBox(0, "", $aLines[1])

That seems to work great, I've not used arrays much before so I didn't think of using them, thank you.

(BTW have applied small fixes to make it work above)

Share this post


Link to post
Share on other sites

Just one other quick question if I may is it possible to use FileFindFirstFile more than one directory deep, E.g. child directories.

Oli

Share this post


Link to post
Share on other sites

The "Cannot be an array element." kills that I think, trying anything involving a variable in the name just throws up Assign^ ERROR

Oli

Doesn't look like you are using arrays.

this throws up an error in scite as the variable is not declared but continuing works anyway

$temp = "Test variable"
$line = "Line"
$CurrentLineNo = "1"
Assign($line & $CurrentLineNo, $temp)
MsgBox(0, "", $Line1)

But yes using an array is better if you understand them.

Rather then using _ArrayAdd i would add to a delimeted string and then string split afterwards, reason being _ArrayAdd uses redim which can be slow on big files.


GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites

Thanks for all your help

Oli

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