Jump to content

Assign - Compile Error for Variables Created


Recommended Posts

I think that I already know the answer but I don't like it too much so I'm looking for any other ideas that might make it better.

Assign seems to be a very little used Autoit feature as there isn't a lot written about it (I've looked). I am using it to create the equivalent of the PHP Extract function. For those who are not familiar with PHP, this takes an Associative array variable (ie it has a name not just an index) and creates a standalone variable using its name and an optional prefix. I am using this in my SQLite_Easy UDF to support the use of associative features in retrieving table data. It's all working quite well with one annoying exception.

The Problem

Since Assign creates variables on the fly and AutoIt wants to have variables defined in advance when it compiles a module, it reports an error whenever you use one of these "on-the-fly" variables. That seems to reduce the value of using Assign since you now have to go through these errors to make sure that there isn't a "real" error buried among these non-errors. The only approach that I can see to avoid these errors would be to pre-define all these variables before using Assign but that is cumbersome and reduces the benefit of using Assign. I can do some automation on that process but it is still a second-best solution.

Ideally, I guess that I would want to be able to suppress these errors, make them a warning instead of an error or even color-code them differently so it is easier to to separate them from "real" errors. Any creative ideas?

Link to comment
Share on other sites

  • Developers

Which specific errors are you referring to, because I doubt they are not given by AutoIt3?

Probably you are talking about AU3Check?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

As far as I know this only happens when you try to directly evaluate a variable that is declared by assign.

This means that you have to know it's name, so there would be no reason to use assign as in this example:

Assign("ErrorVar",Random(100,999,1))
ConsoleWrite("$ErrorVar has the value " & $ErrorVar & @CRLF)

I get no errors if I use Eval to check the contents of variables created by assign, like this:

Global $aVarNames[10]
For $i = 0 To 9
    $aVarNames[$i] = "Var" & Random(1000,9999,1)
    Assign($aVarNames[$i],Random(100,999,1))
    ConsoleWrite($aVarNames[$i] & " has the value " & Eval($aVarNames[$i]) & @CRLF)
Next

Additional AU3Check parameters seem to make no difference. Do you have an example?

Link to comment
Share on other sites

There are two ways to hide the Au3Check-warnings, although you could just ignore them. forcedef only works with the beta of Au3Check, though.

#forcedef $test
Assign("test", 12)
ConsoleWrite($test & @LF)

;...

Global $test
Assign("test", 12)
ConsoleWrite($test & @LF)

Or use a Dictionary-Object instead of variables.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

  • Developers

So, what is the problem?

Au3Check just gives you a warning for all variables that it doesn't find being declared. Just ignore them when you are sure they are correct.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

#forcedef $test
Assign("test", 12)
ConsoleWrite($test & @LF)

This is only available in the current Beta. :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks for all the feedback. Here's an example of the problem:

; experiment with Assign

$variable   = "test_var"
$value      = "12345"

    Assign ($variable,$value)
    consolewrite ("Value of test_var is " & $test_var)

This gives an error on the use of $test_var. I am actually doing the assign in a function that generates a bunch of variables (and sets the values) based on the column names in the DB Table so there is a value to doing it.

#forcedef will be a good answer once it becomes part of the standard distribution and if the definition to be forced is the content of the variable (which I doubt) instead of the variable itself (sort of like Eval or Execute). If it has to be hard-coded, then I'm no further ahead.

Yes, I could just ignore the errors (and I have in my testing) but in a large program where these variables could be used a lot, it will generate a lot of errors. If you just automatically ignore them, then chances are that there will be other "real" errors that will get overlooked as well. It slows down and complicates the development/debugging process.

Link to comment
Share on other sites

  • Developers

When a #forceDef is fine for you then why not use a Global definition for the Variables used in Assign()?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I've never used a dictionary but I had a quick look and that might be a solution. I will have to do some investigation and experimenting. One limit is that it can only handle text so that might be too limiting. A good suggestion though and I will look into it further.

Link to comment
Share on other sites

The problem with adding the Global variables is that it takes something that is automated and requires only one function call to generate a bunch of variables and values and now adds the requirement to start hard-coding a bunch of global statements. This takes away much of the benefit.

Link to comment
Share on other sites

  • Developers

So why do you need this "one function call to generate a bunch of variables" anyway?

Couldn't you simply use an Array for that into one Variable?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Since AutoIt doesn't provide native support for Associative Arrays, I'm trying to work around that by providing some functions such as this one that buffer the user program from hard-coded indexes. Since the underlying array is built from a Database table, if you use hard-coded indexes, you hard-wire the program to the structure of the database table. If you change the database table (e.g insert a new column in the middle) then your program has to be changed because the indexes are no longer correct. That is a very undesirable situation. You don't really want to be in the situation where you can only modify a table by adding fields to the end. It's better if you can logically group fields within the table. My frame of reference for this is my experience with PHP and MySQL where this isn't an issue because PHP provides native support for Associative Arrays and the MySQL interface can use it. The PHP Extract command does exactly what I'm trying to do here. There are three simple ways that you can code a reference to a field in PHP.

$tab1_first_name (after doing an Extract)

 $tab1['first_name'] (Using an associative approach based on the column name)

 $tab1[5]  (the most basic approach using the column offset)

I have already developed a function that provides the equivalent of the second format (since it can't be done directly). I am trying to provide the first option as well to complete the toolkit in this regard. As mentioned at the beginning, it works fine except for the annoying error message.

Edited by chrshea
Link to comment
Share on other sites

If you change the database table (e.g insert a new column in the middle) then your program has to be changed because the indexes are no longer correct. That is a very undesirable situation.

You should always specify the fields you want to fetch in the querystring. Then you don't have to worry about the order of the fields in the table.

Anyways, I'd prefer a dictionary object here. It even has a abbreviated syntax which is more array-like:

$oD = ObjCreate("Scripting.Dicitionary")
$oD("col1") = "test"
$oD("col2") = 12
ConsoleWrite($oD("col1") & @CRLF

Edit: Assign in AutoIt is not anywhere near extract in php. You always have to create global variables and that could lead to some unexpected results.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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