Jump to content
Burgs

Dynamic Variable Creations?

Recommended Posts

Burgs

Hello,

  I'd like to be able to create variables 'on the fly' by reading in some data from an SQLite database.  Using this data i'd like to create variables.  The data would be separated by colons (":")...'colon delimited'.  I would perform a 'StringSplit' on the data to create arrays holding each colon separated value.  Ideally I could then use these strings to create variables from them...possible?

  I know it cannot be done just piecing a string together such as "var = "$_" & 'some string'"... 

  I think it might work using the 'Assign' command...and there is an 'Opt' option to 'ExpandVarStrings' which seems to indicate it is possible to include a "$" within a string...however the documentation is a bit spotty on that.

  Can anybody confirm that these might be the best practices to create variables dynamically?  ...Perhaps there is another method I'm not thinking of...?   I thank you in advance.  Regards.  

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

Good morning @Burgs:)
Yes, you can dinamically create variables and use them in your script.
The Assign() function needs to create the variable and assign a value to it. You can see an example of its usage in the Help file, which, in my honest opinion, is one of the most intuitive and complete helps I've ever seen for a programming language.
To retrive the value of the variable, you need to use Eval() function.

Take a look at the example below:

 

Local $strVariables = "intVariable:fltVariable:strVariable", _ ; In your case
      $arrVariables

; Split variables and assign them to the $arrVariables array
$arrVariables = StringSplit($strVariables, ":", $STR_NOCOUNT)

For $i = 0 To UBound($arrVariables) - 1
    Switch $i

        Case 0
            ; $ASSIGN_CREATE is set by default from the function, so you can easily omit that parameter
            If Assign($arrVariables[$i], 5, $ASSIGN_CREATE) Then
                ConsoleWrite("Variable " & $arrVariables[$i] & " successfully created." & @CRLF)
            Else
                ConsoleWrite("Errore during the creation of the Variable " & $arrVariables[$i] & @CRLF)
            EndIf

        Case 1
            If Assign($arrVariables[$i], 10.5, $ASSIGN_CREATE) Then
                ConsoleWrite("Variable " & $arrVariables[$i] & " successfully created." & @CRLF)
            Else
                ConsoleWrite("Errore during the creation of the Variable " & $arrVariables[$i] & @CRLF)
            EndIf

        Case 2
            If Assign($arrVariables[$i], "This is a string", $ASSIGN_CREATE) Then
                ConsoleWrite("Variable " & $arrVariables[$i] & " successfully created." & @CRLF)
            Else
                ConsoleWrite("Errore during the creation of the Variable " & $arrVariables[$i] & @CRLF)
            EndIf

    EndSwitch
Next

MsgBox($MB_ICONINFORMATION, "", "Integer Variable Value: " & Eval($arrVariables[0]) & @CRLF & _
                                "Floating Variable Value: " & Eval($arrVariables[1]) & @CRLF & _
                                "String Variable Value: " & Eval($arrVariables[2]))

So, please, pay attention when read the Help file :)

Best Regards.
 

Edited by FrancescoDiMuro
Inserted the split of the variables :)

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
jchd

@Burgs,

Don't do that. You're going to make your code uselessly complex and difficult to read/maintain for no good reason.

Assign & Eval are only useful in a very few specific situations and yours isn't one of them. Work with arrays and if you feel uncomfortable with numerical indices, use Enum to makes them verbose and user-friendly.

  • Like 3

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
junkew

Whats your endgoal. I agree with assign/eval is not frequently needed most likely only when you are writing a udf it can be handy.

Maybe another option if you want to have some data abstraction is to use an object layer although that would also create some overhead compared to straight array processing

 

 

Share this post


Link to post
Share on other sites
Burgs
Posted (edited)

Hello to all,

  Thank you for the replies.  FrancescoDiMuro yes I agree about the manual documentation...it is well done and i've always been happy with the explanations contained therein.  I'm more than happy to use arrays, that would be my preferred method...however how can I convert such a string into a variable?  I believe I need to make usage of the 'ExpandVarStrings'  ("Opt" command)...however I have no idea how to do it since that is one of the few items in the documentation that does not seem well explained... 

  Can that option be used to read in array string values that contain "$" in them?  Then perhaps they can be used as variables...?  For example:

;Suppose I have read 'colon delimited' data in from a database...:

$_import_data = "lions:tigers:bears:oh_my"  ;string of colon-seperated values read in from a database 
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data

  ;...how can these strings be converted into variables...?
  
  ;...is this possible...?
  
Opt("ExpandVarStrings", 1) ;0=don't expand, 1=do expand
  
$_import_data = "$_lions:$_tigers:$_bears:$_oh_my"  ;string of colon-seperated values (with "$_" prefix) read in from a database 
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data

 For $x = 0 to Ubound($_raw_data) - 1
 String($_raw_data[$x]) = "abc"  ;assign each 'imported' variable some value
 Next

does that seem feasible...?  or something similar?  Thanks in advance. 

Edited by Burgs

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

Good morning @Burgs:)
What is not clear for you in the code I posted?
The code I posted ALREADY creates variables from a given string, which is splitted into an array of "variables".
Then with the For...Next loop, the variables are created.
By the way, your code above is wrong, and I want to explain you why.

 

;Suppose I have read 'colon delimited' data in from a database...: ( And is what I have supposed )

$_import_data = "lions:tigers:bears:oh_my"  ;string of colon-seperated values read in from a database ( Same )
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data ( Same )

  ;...how can these strings be converted into variables...? ( With Assign, as I've explained to you with the script I posted )
  
  ;...is this possible...? ( Yes, it is )
  
; This Option is used to do things like these:
; With Opt("ExpandVarStrings", 0) => ConsoleWrite("The value of the string is: " & $strString & "." & @CRLF)
; With Opt("ExpandVarStrings", 1) => ConsoleWrite("The value of the string is: $strString$. @CRLF")
; And, in my honest opinion, I prefer to separate "static" strings from variables.

; Explanation of this Option above:
Opt("ExpandVarStrings", 1) ;0=don't expand, 1=do expand 

; With this part of code, you just do what you already did above, plus the symbol $...
  
$_import_data = "$_lions:$_tigers:$_bears:$_oh_my"  ;string of colon-seperated values (with "$_" prefix)read in from a database 
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data

; With this part of code, you don't assign anything to any variables, because you ain't CREATED the variables.
; And, as I've already explained to you, to create the variables and assign them a value, you have to use Assign() function.

 For $x = 0 to Ubound($_raw_data) - 1
    ; Here, you are assigning a value to the ELEMENT of the array, overwriting your "variables" set.
    $_raw_data[$x] = "abc"  ;assign each 'imported' variable some value
 Next

So, please, read CAREFULLY what users suggest to you.
I don't know how to be more exhaustive than I've been.

Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Burgs

Hello,

  Thank you for the response.  My code was only theoretical, just exploring options on how to possibly go about this.  I believe I understand your code well enough....so I could do something such as:

$_import_data = "lions:tigers:bears:oh_my"  ;string of colon-seperated values (with "$_" prefix) read in from a database 
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data

 For $x = 0 to Ubound($_raw_data) - 1
  if IsString($_raw_data[$x]) == 1 Then  
  Assign($_raw_data[$x], "some value", $ASSIGN_CREATE + $ASSIGN_FORCEGLOBAL)  ;assign each 'imported' variable some value
  Else
  ConsoleWrite("Errore during the creation of the Variable...'raw_data' is not a 'string'" & @CRLF)
  EndIf 
 Next

 What about that line in the documentation (for the "Assign" command) that states (for 'varname' parameter)   "...cannot be an array element..."  ???  That would seem to prevent usage in this manner...?  However in the code you provided it works (I ran it)  ...why?

  Is this the best way to go about this...it is a bit troublesome to have to use "Eval" when reading these created variables...

  However the main thing is I want the 'imported' string ('$_raw_data') in my example...to be the name of the variable...not the '$_raw_data' array element...thus in my code sample I want "lions", "tigers", "bears", "oh_my" to be the variable NAMES...from which I can assign values to them...such as:

$_lions = "Elsa"  ;assign a string value to "$_lions"  

$_tigers = "tigger the tiger"  ;assign a string value to "$_tigers"

$_bears = "Winnie"  ;assign a string value to "$_bears"

$_oh_my = 78  ;assign a numeric value to "$_oh_my"

Can that be accomplished?

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@Burgs

if IsString($_raw_data[$x]) == 1 Then

First, the operator == is used only for compare two strings, with case-sensitive comparsion, as the Help states:

== Tests if two strings are equal. Case sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case sensitive.

Second:

4 minutes ago, Burgs said:

What about that line in the documentation (for the "Assign" command) that states (for 'varname' parameter)   "...cannot be an array element..."  ???

That probably is an error in the Help file, or I ddidn't understand what that phrase in the Help file was meaning.

Third:

7 minutes ago, Burgs said:

However the main thing is I want the 'imported' string ('$_raw_data') in my example...to be the name of the variable...not the '$_raw_data' array element...thus in my code sample I want "lions", "tigers", "bears", "oh_my" to be the variable NAMES...from which I can assign values to them...such as:

$_lions = "Elsa"  ;assign a string value to "$_lions"  

$_tigers = "tigger the tiger"  ;assign a string value to "$_tigers"

$_bears = "Winnie"  ;assign a string value to "$_bears"

$_oh_my = 78  ;assign a numeric value to "$_oh_my"

I dind't understand. Please explain it better, maybe using the Code tag.

Best Regards.


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Burgs

Hi,

  Yes I know about the "==" thing...I do it out of habit...and I prefer doing it that way for 'comparisons' between two variables...I always use the double ("==") because I feel it is easier to read and distinguish 'comparisons' from 'assignments'...thus:

 if $a == $b Then   ;easy to see this is a comparison

$a = $b   ;easy to see that '$a' is assigned the value of '$b'

That is just how I always do it...  hehe

  That last part is what I mean by 'dynamic variables'...I want to create variables from imported database strings...as I have there.  "lions", "tigers", "bears", "oh_my" should be the variable names...I am guessing at this point there is probably no way to do that...I suppose I could do it the other way...would just be nice to create variables from external source data...thanks again for your replies  !

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@Burgs

59 minutes ago, Burgs said:

if $a == $b Then   ;easy to see this is a comparison

$a = $b   ;easy to see that '$a' is assigned the value of '$b'

NOT if you have a If near your condition.

And, about:

59 minutes ago, Burgs said:

That last part is what I mean by 'dynamic variables'...I want to create variables from imported database strings...as I have there.  "lions", "tigers", "bears", "oh_my" should be the variable names...I am guessing at this point there is probably no way to do that...I suppose I could do it the other way...would just be nice to create variables from external source data...thanks again for your replies  !

This is possibile, but or you are so persistent to not understand the script I wrote to let you understand the mechanism of create dynamically variables, or you are meaning something else, but I still don't understand what you were/are meaning.
By the way, I'm out of this.
Maybe someone else could understand your question, and help you more than I feel I did.

Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Burgs
Posted (edited)

OK up to you...I do appreciate your help...I am probably not understanding...I am not very intelligent...  hehehe

can I assume this code (modified from above) will create variables named from the actual strings...?:

$_import_data = "lions:tigers:bears:oh_my"  ;string of colon-seperated values (with "$_" prefix) read in from a database 
$_raw_data = StringSplit($_import_data, ":")  ;create an array of the strings in $_import_data

 For $x = 0 to Ubound($_raw_data) - 1
  if IsString($_raw_data[$x]) == 1 Then  
  Assign(String($_raw_data[$x]), "some value", $ASSIGN_CREATE + $ASSIGN_FORCEGLOBAL)  ;assign each 'imported' variable some value
  Else
  ConsoleWrite("Errore during the creation of the Variable...'raw_data' is not a 'string'" & @CRLF)
  EndIf 
 Next
 
 ;and now I can return the values of each using "eval"...
 
 $sEvalString = Eval("lions")
 $sEvalString2 = Eval("tigers")
 $sEvalString3 = Eval("bears")
 $sEvalString4 = Eval("oh_my")
 
 ;...and that should give me whatever values have been assigned to each variable...? 
 ;such as "Elsa", "Tigger the Tiger", "Winnie", 78

sorry for any confusion on my part...I understand if you don't wish to reply.,,I will play around with it when I have the time ...thanks again for your assistance. 

Edited by Burgs

Share this post


Link to post
Share on other sites
pixelsearch
Posted (edited)

@Burgs : what about this ?

$_import_data = "lions:tigers:bears:oh_my"
$_raw_data = StringSplit($_import_data , ":")

For $x = 1 to $_raw_data[0] ; $_raw_data[0] contains the number of elements
   Assign("_" & $_raw_data[$x], $x)
Next

MsgBox(0, "Test", _
   "$_lions = " & $_lions & @CRLF & _
   "$_tigers = " & $_tigers & @CRLF & _
   "$_bears = " & $_bears & @CRLF & _
   "$_oh_my = " & $_oh_my)

  Test.jpg.54340258505509229d8438476c307b12.jpg
 

Edited by pixelsearch
tried an image upload

Share this post


Link to post
Share on other sites
jchd

@Burgs

What we collectively don't understand is your requirement to dynamically create variables with specific names. That a variable is refered to by $a, $_My_Favorite_Variable, $aMyArray[43] or whatnext is irrelevant to the behavior of code.

In your example, instead of dealing with pieces of your SQL group_concat output, you could as well retrieve the wanted data using a simple "select X from... group by X ..." and get an array as result, say it's assigned to $aResult. Then your code would use $aResult[0], $aResult[1], ..., $aResult[n] instead of the Assign/Eval ugly and useless machinery.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Burgs

Hello,

  pixelsearch...I think that nails it...!   I tried it out and it seems perfect...thank you so much for that...!  Now I just have to figure out how that works...I mean why does the first parameter in the "Assign" command ( "_" & $_raw_data[$x] ) know to prefix a "$" before the rest...?  I guess that must be part of the 'Assign' command functionality?

  jchd...the reason is that I want to create a 'template' program that will pull the strings from a database and be used to populate variables, controls, other information, etc...  to create/populate SQLite databases and tables in a 'creation' program...I want the variables to have meaningful names that I can plug into the 'master' database (the colon delimited strings from my initial post)...they can be used to create SQLite table names and table column headings, for example.  Yes of course I realize it doesn't matter to the code what the names are...just a personal preference...I don't want to deal with 'generic' variable names... 

  Thanks again to all that responded...that is much appreciated.

Share this post


Link to post
Share on other sites
pixelsearch
Posted (edited)

Glad it helped, Burgs. When things seem complicated, just restart from scratch with the simplest script you can think of!

By the way , concerning the question you asked to Francesco :

Quote

Burgs said:

What about that line in the documentation (for the "Assign" command) that states (for 'varname' parameter)   "...cannot be an array element..."  ???

That probably is an error in the Help file, or I ddidn't understand what that phrase in the Help file was meaning.

I dont think it's an error in the help file, look at the following script :

Global $sString = "hello", $aArray[4] = [0, 1, 2, 3]

Assign("sString", "goodbye")
Assign("aArray[1]", -111)

MsgBox(0, "Test 2", _
   "$sString = " & $sString & @CRLF & _
   "$aArray[1] = " & $aArray[1])

  5b28ffbb8478f_Test2.jpg.d14e3d24141e3180ee9356be035b558d.jpg
Please note that $sString was equal to "hello", then Assign changed it to "goodbye", ok ?

But $aArray[1] was equal to 1 and is still equal to 1 after the Assign, it didn't change to -111

I guess that's what the help file means ; varname, the 1st parameter of the Assign function, "cannot be an array element" (i.e when you place it into quotes like in this Test 2). It's different from my precedent post ("Test") where $_raw_data[$x] wasn't into quotes because we wanted its value to become a variable name, and it worked.

 

Edited by pixelsearch
plenty of empty lines at the end of the post :(

Share this post


Link to post
Share on other sites
jchd

@Burgs

You're going to make your life way more difficult than needed but that's your own choice.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Burgs

pixelsearch yes you probably nailed it again...the documentation just worded a bit vague I guess.

jchd yes perhaps but I guess that is how things have gone for me for the past 50 years...hahaha

  If I needed to write a number of sub-programs I would need to be able to create variables for them dynamically, no...?  In theory (I have not attempted it at this point) it should be possible to write programs from a template using the various 'file' commands...I would think 'fileread', 'filewrite', 'filewriteline', etc would work with SciTE...? 

thanks again to all...!

Share this post


Link to post
Share on other sites
jchd

Honestly I've never encountered such a need in any development context I know of. Of course languages Lisp-based often use deep introspection but we aren't there here.

Also I see no possible relationship between subprograms and your practice.

Perhaps we would understand each other a bit more if you could post a real-world simple example of what you have in mind and why this way is the only solution from your point of view.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

  • Similar Content

    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
       
      <autoit>
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      </autoit>
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      <autoit>
      Assign($clear1, $SATJ)
      </autoit>
       
      any help is greatly appreciated!!! 
    • MrCheese
      By MrCheese
      Hey Guys,
       
      So, the functions work, where primarygui() accurately determines the evaluation of the status of the checkboxes - the msgbox picks this up.
       
      However, later on, when we re-enter a 'for $i = 0 to ubound($checkbox)' loop, then in the 'batchinitial' function it doesn't picked up that the status&$i = 1, so it jumps out, then within the While 1 loop, it exits the loop in the first row, again because the status& $i= 0
      The "assign" line within the primarygui funtion, is this only a local assignment? if so, how can I make it cross function?
       
      Thank in adv for your help
       
       
      Func excelsheetlist() $i = 0 Global $aWorkSheets = _Excel_SheetList($oWorkbook1) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_SheetList Example 1", "Error listing Worksheets." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;_ArrayDisplay($aWorkSheets, "Excel UDF: _Excel_SheetList Example 1") ;_ArrayDisplay($aWorkSheets, "Array") Global $iRows = UBound($aWorkSheets, $UBOUND_ROWS) ; Total number of rows. In this example it will be 10. Global $iCols = UBound($aWorkSheets, $UBOUND_COLUMNS) ; Total number of columns. In this example it will be 20. Global $iDimension = UBound($aWorkSheets, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. MsgBox($MB_SYSTEMMODAL, "", "The array is a " & $iDimension & " dimensional array with " & _ $iRows & " row(s) & " & $iCols & " column(s).") Dim $checkbox[$iRows] EndFunc ;==>excelsheetlist Func primarygui() ; Create a GUI with various controls. Local $hGUI = GUICreate("Script Controller", 300, ($iRows * 24)) ; Create a checkbox control. ;Local $idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25) Local $Button2 = GUICtrlCreateButton("Close", 210, 200, 85, 25) Local $Button3 = GUICtrlCreateButton("Run", 210, 170, 85, 25) Local $Button1 = GUICtrlCreateButton("Discharge", 210, 140, 85, 25) For $i = 0 To UBound($checkbox) - 1 $checkbox[$i] = GUICtrlCreateCheckbox($aWorkSheets[$i][0], 8, 0 + ($i * 24)) ;, 81, 17) Next ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 $nMsg = GUIGetMsg() Select Case $nMsg = $GUI_EVENT_CLOSE Exit Case $nMsg = $Button2 ;Close Exit Case $nMsg = $Button1 MsgBox(0, "Discharge Button not configured", "Now Exiting") Exit Case $nMsg = $Button3 ;Run $fSelection = False For $i = 0 To UBound($checkbox) - 1 If BitAND(GUICtrlRead($checkbox[$i]), $GUI_CHECKED) Then $fSelection = True ExitLoop EndIf Next If $fSelection Then For $i = 0 To UBound($checkbox) - 1 Assign("status" & $i, GUICtrlRead($checkbox[$i])) Next $batchcount = 0 For $i = 0 To UBound($checkbox) - 1 If Eval("status" & $i) = 1 Then $batchcount = $batchcount + 1 ;Call ("o" & $i & "copy") ; if you want to call the functions directly, remove ; before the call and comment or delete the following DirCopy statement MsgBox(0, "Checking", "Checking that: " & $checkbox[$i] & " no, with title: " & $aWorkSheets[$i][0] & " was selected, Batch count: " & $batchcount) ; if you need only the DirCopy EndIf Next ExitLoop Else MsgBox(48, 'No Items Selected', 'You have not selected any Patients to Load, Please select from the list') EndIf EndSelect WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>primarygui Func _IsChecked($idControlID) Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED EndFunc ;==>_IsChecked Func batchinitial() If Eval("status" & $i) = 1 Then debugbox() $oWorkbook1.Sheets(1).Activate $bigloop = $bigloop + 1 $sheet = $aWorkSheets[$i][0] $oWorkbook1.Sheets($sheet).Activate debugbox() EndIf EndFunc ;==>batchinitial OpenExcel() excelsheetlist() primarygui() For $i = 0 To UBound($checkbox) - 1 batchinitial() While 1 If Eval("status" & $i) = 0 Then ExitLoop ;all the rest of my script loops etc WEnd Next  
    • SadBunny
      By SadBunny
      The following code:
      Opt("ExpandVarStrings", True) $string1 = "something" $string2 = "another thing" $try = "this" $stringToUse="string1" ; Test bit 1 ConsoleWrite("=====" & @CRLF) ConsoleWrite("Test: '$try$', '$" & $stringToUse & "$'@CRLF@") ConsoleWrite("-----" & @CRLF) ; Test bit 2 ConsoleWrite("=====" & @CRLF) ConsoleWrite("Test: '$string2$'@CRLF@") ConsoleWrite("-----" & @CRLF) ... produces:
      Main question: can I read the variable to read from from a variable, like I tried in "Test bit 1"? (If not, then why?)
      Second question: if I cannot and if it is because the string expansion is done at "compile time" or something, why does it break the expansion of the "@CRLF@" after the concatenated string as well (but not before - the $try$ is expanded just fine), but then still just outputs a string and doesn't throw any errors?
      I don't have a particular thing I'm trying to accomplish; another question about language files just made me remember that I used this option long ago and it helped me greatly for that purpose. This to say that I don't need other approaches. I'm just playing around - I probably wouldn't use the ExpandVarStrings for anything serious at this point anyway 
    • aleeksunder
      By aleeksunder
      Hello!
      Can anyone explain what's going on here:
      ; Initializing an array Local $a = [ 0 ] ; Lately in code I want to assign new array to $a variable $a = [ 1 ] and got this error at compile time
      ==> Error in expression.: $a = [ 1 ] $a = ^ ERROR What is it and why?
      And I've mentioned that I can't pass "manual" array to function as parameter:
      a( [ 0 ] ) Func a( $a ) _ArrayDisplay( $a ) EndFunc still the same error
      ==> Error in expression.: a( [ 0 ] ) a( ^ ERROR  
    • algiuxas
      By algiuxas
      Hello,
      I need to assign data to array:
      Global $Test[3][4] $Array = "Test" SetArrayData($Array,"Hello world!",1,3) ConsoleWrite($Array[1][3]&@CRLF) Exit Func SetArrayData($Array,$Data,$1,$2) $Array[$1][$2] = $Data; <-- ??? EndFunc I need that somebody would help me with that function
×