Jump to content

Recommended Posts

Posted

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.  

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

ALWAYS GOOD TO READ:

 

Posted

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

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)

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

ALWAYS GOOD TO READ:

 

Posted

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?

Posted

@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

ALWAYS GOOD TO READ:

 

Posted

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  !

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

ALWAYS GOOD TO READ:

 

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

@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)

Posted

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.

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 :(
Posted

@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)

Posted

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

Posted

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)

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
×
×
  • Create New...