Jump to content

create multiple variables with a while loop


Recommended Posts

Local $counter = 0
While $counter < 5
    $var$counter = 1+$counter
    $counter=$counter+1
WEnd

GUICreate("test")
GUISetState()
GUICtrlCreateLabel($var0, 5, 20)
GUICtrlCreateLabel($var1, 5, 40)
GUICtrlCreateLabel($var2, 5, 60)
GUICtrlCreateLabel($var3, 5, 80)
GUICtrlCreateLabel($var4, 5, 100)

i would like my $var to have the counter number added to its name. is this even possible if it is how?

Link to comment
Share on other sites

Using arrays makes it much easier than adding the index to the variable name.

Means: Use $var[0] instead of $var0.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Does this help at all?

Local $var[1], $counter = 0
While $counter < 5
    $var[$counter] = 1 + $counter
    $counter = $counter + 1
    ReDim $var[UBound($var) + 1]
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next
Link to comment
Share on other sites

 

Does this help at all?

Local $var[1], $counter = 0
While $counter < 5
    $var[$counter] = 1 + $counter
    $counter = $counter + 1
    ReDim $var[UBound($var) + 1]
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next

 

Maybe Im wrong, but i think changing an array size inside a loop is a not a good thing.

Instead it should create at start an array with the final size. If the amount of elements that will be assigned to the array is unknow before run the program, then you should use a guess value for the array size and after the loop use the ReDim to reduce the array size.

In this case the final size is know, so the code could be:

$max = 5
Local $var[$max], $counter = 0
While $counter < $max
    $var[$counter] = 1 + $counter
    $counter += 1
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter-1
   GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next
 
sleep(3000) ;Time to see the GUI before the program end
Link to comment
Share on other sites

If the amount of elements that will be assigned to the array is unknow before run the program, then you should use a guess value for the array size and after the loop use the ReDim to reduce the array size.

This "guessing" the size of an unknown array could cause for a "Array variable has incorrect number of subscripts or subscript dimension range exceeded" error and that could lead to it not working at all.  There is nothing wrong with re-sizing an array within a loop.  If there is a known amount then you could set the size at the beginning and not use another variable as follows:

Local $var[6], $counter = 0
While $counter < UBound($var) - 1
    $var[$counter] = 1 + $counter
    $counter += 1
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next
Link to comment
Share on other sites

  • 5 months later...

Is there a way to create directly the name of the variables in loop ? like something like that "dim $p$i" where $i increase each time ?

Edited by akorx

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

That's what arrays are being made for.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...