Jump to content

how to dim a variable correctly


jennico
 Share

Recommended Posts

hi,

i am a little bit confused having read so much about dim and dont get it working.

here`s my script designed to count and read out ini files from a plugin directory. i want to have results like this:

($file[1] = "plugin1.ini")

($file[2] = "plugin2.ini") and so on.

Dim $file[22]

$f=1
$pfad=0
$search = FileFindFirstFile(@ScriptDir&"\desti\Plugins\*.ini")          
    If $search = -1 Then $pfad=1
    If @error Then $pfad=1  
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        MsgBox(4096, $f&".File:", $file)
        $file[$f]=$file
        If $f=22 Then ExitLoop
        $f=$f+1
    WEnd
    $f=$f-1
    If $f > 22 Then $f=22
    FileClose($search)

what is wrong ?

j.

Error: badly formatted variable or macro line $file[$f]=$file

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

um it's not the dim command having the problem, it's probably the fact that you have two $file variables, one is an array, and the other a dynamic string that changes with each loop.

First you have $file[22] declaring $file as an array, then you define $file = FileFindNextFile($search), defining it as a string of the filename.

Then you expect $file[$f]=$file to work? it won't, because $file[22] doesn't exist anymore because you replaced it with a string.

give string $file a different name than array $file[]

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

Or in other words, something like this:

Global $aFile[22]
Global $iCount = 0

$hSearch = FileFindFirstFile((@ScriptDir & "\desti\Plugins\*.ini")
if $hSearch = -1 then
  MsgBox(0, "Error", "No files/directories matched the search pattern")
  Exit
endif

while $iCount < UBound($aFile)
  $sFile = FileFindNextFile($hSearch)
  if @Error then ExitLoop
  $aFile[$iCount] = $sFile
  $iCount = $iCount + 1
wend
FileClose($hSearch)

for $iI = 0 to $iCount - 1
  MsgBox(4096, "File", $iI & ".File: " & $aFile[$iI])
next
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

thanx for that:

", it's probably the fact that you have two $file variables, one is an array, and the other a dynamic string that changes with each loop. "

i just renamed the $file, and it works

Dim $ini[22]

$f=1
$pfad=0
$search = FileFindFirstFile(@ScriptDir&"\desti\Plugins\*.ini")          
    If $search = -1 Then $pfad=1
    If @error Then $pfad=1  
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        MsgBox(4096, $f&".File:", $file)
        $ini[$f]=$file
        If $f=22 Then ExitLoop
        $f=$f+1
    WEnd
    $f=$f-1
    If $f > 22 Then $f=22
    FileClose($search)

i would have never thought that this is a problem. in my logic $file and $file(1) are different things, the one being a variable and the other an array. for autoit not. good to know.

thanx a lot

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

  • 5 months later...

Is there any wat to declare an array without specifiy exactly the number.

Global $aFile[22] ::::::> [b]Here i don´t want to put the number 22, only want to put something that depends exactly to the files founded in the following search[/b]
Global $iCount = 0

$hSearch = FileFindFirstFile((@ScriptDir & "\desti\Plugins\*.ini")
if $hSearch = -1 then
  MsgBox(0, "Error", "No files/directories matched the search pattern")
  Exit
endif

while $iCount < UBound($aFile)
  $sFile = FileFindNextFile($hSearch)
  if @Error then ExitLoop
  $aFile[$iCount] = $sFile
  $iCount = $iCount + 1
wend
FileClose($hSearch)

for $iI = 0 to $iCount - 1
  MsgBox(4096, "File", $iI & ".File: " & $aFile[$iI])
next
Edited by Delai
Link to comment
Share on other sites

hi,

i am a little bit confused having read so much about dim and dont get it working.

here`s my script designed to count and read out ini files from a plugin directory. i want to have results like this:

($file[1] = "plugin1.ini")

($file[2] = "plugin2.ini") and so on.

Dim $file[22]

$f=1
$pfad=0
$search = FileFindFirstFile(@ScriptDir&"\desti\Plugins\*.ini")          
    If $search = -1 Then $pfad=1
    If @error Then $pfad=1  
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        MsgBox(4096, $f&".File:", $file)
        $file[$f]=$file
        If $f=22 Then ExitLoop
        $f=$f+1
    WEnd
    $f=$f-1
    If $f > 22 Then $f=22
    FileClose($search)

what is wrong ?

j.

Error: badly formatted variable or macro line $file[$f]=$file

One more thing, that you will run into after fixing the ambiguous variable names $file and $file[]: You dim $file to have 22 elements. This means they will reach from 0 to 21. Since you let the loop exit on 22 but before the loop exits, you do $file[$f]=file, you will run into the problem that the program tries to write something into element 22 which does not exist. To fix this, either dim your array one element bigger, or make the loop exit at count 21, or put the $file[$f]=file statement after the if $f=22 check.

/edit: but anyway, if you know the amount of loops beforehand, I would always go for a for-next loop. It saves you the need to keep a counter and makes your code shorter and more readable.

/edit 2: but I see that PaulIA already suggested that :whistle:

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

My question was different because i don´t know the amount of loops before.

That's why I replied to jennico and not to you :whistle:

As for your question, you can do for instance Dim $array[1] or Dim $array and later use ReDim to add extra element slots to the array.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...