[22 Sep 2009] importing tables

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
GMKai
Posts: 158
Joined: Mon Feb 15, 2010 10:58 am

[22 Sep 2009] importing tables

Post by GMKai »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 22 Sep 2009 11:21:53 +0200

Hello,

I fixed the previously mentioned problem, when some entries were skipped and the current project had to be smaller the one which should be imported. There seems to be a problem with variables and their scope, in the below example, which works now, I use "DebugPrint("counter..", i)" this works, but when useing i in the for-loop before this lead to problems for the entries 0, 1 and 2.

As I have had this problems before, I would like to know what to do about it. Am I encouraged to make more frequent use of the "local"-statement or should i get hold of the used helper-variables and change these throughout the project?

Code: Select all

Function p_ExecImport()
    Global imported = 0
    ;Nation
    If ImpNationCount > 1
       For counter = 0 To (ImpNationCount-1) Step 1
           DebugPrint("NC..",NationCount)
           DebugPrint("counter..", i)
           p_defTab(t_nation, NationCount,NationCount+1, 1)
           newid = p_CreateID()
           t_nation[NationCount][0]= newid
           t_nation[NationCount][1]= t_IMPnation[counter][1]

           t_impRef[imported][0] = t_IMPnation[counter][0]
           t_impRef[imported][1] = newid

           imported    = Add(imported,   1)
           NationCount = Add(NationCount,1)
       Next
    ElseIf(ImpNationCount = 1)
       p_defTab(t_nation, NationCount,NationCount+1, 1)
       newid = p_CreateID()
       t_nation[NationCount][0]= newid
       t_nation[NationCount][1]= t_IMPnation[0][1]

       t_impRef[imported][0] = t_IMPnation[0][0]
       t_impRef[imported][1] = newid

       imported    = Add(imported,   1)
       NationCount = Add(NationCount,1)
    EndIf() 
User avatar
airsoftsoftwair
Posts: 5834
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[25 Sep 2009] Re: importing tables

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 25 Sep 2009 11:34:49 +0200
Hello,

I fixed the previously mentioned problem, when some entries were skipped and the current project had to be smaller the one which should be imported. There seems to be a problem with variables and their scope, in the below example, which works now, I use "DebugPrint("counter..", i)" this works, but when useing i in the for-loop before this lead to problems for the entries 0, 1 and 2.

As I have had this problems before, I would like to know what to do about it. Am I encouraged to make more frequent use of the "local"-statement or should i get hold of the used helper-variables and change these throughout the project?
Well, I don't know what's wrong in your code but it's most certainly a programming mistake :)

As a general rule, you should always use the Local statement for variables that you don't need to have globally. That should apply to most of your variables. Global variables should really be avoided...
GMKai
Posts: 158
Joined: Mon Feb 15, 2010 10:58 am

[25 Sep 2009] Re: importing tables

Post by GMKai »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 25 Sep 2009 13:37:07 +0200

Hello Andreas,

yes you are right with avoiding global variables.

But what am I suposed to do with "helper-variables" like the "i" in for i = 0 to ....
From what I have understood the i is available within the loop and it may last even longer, but when repeating this loop-construct it should not confuse the interpreter. In my case changing the name of the variable helped, but the question is, why is it needed to circumvent the helpervariable when it is suposed to be in it's place. Is the scope of a variable 100%-correct under any circumstances?
I don't want to sound harsh or blame Hollywood/LUA, but this is not the first time I come across this "scopeproblem" and fixed it with just renaming the variable, but sooner or later I will run out of names for variables...and of cause the beauty of the sourcecode doesn't get better with this...

As I can't give a simple example were the variables are getting wrong I would like to ask If passing a variable via "Return(i)" can have an negative effect of how loops do work with the helpervariable?
User avatar
airsoftsoftwair
Posts: 5834
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[25 Sep 2009] Re: importing tables

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 25 Sep 2009 13:59:53 +0200
Hello Andreas,

yes you are right with avoiding global variables.

But what am I suposed to do with "helper-variables" like the "i" in for i = 0 to ....
Just use

For Local i = 0 To ....

and then "i" will only be valid inside the loop's scope. Using a global iterator variable is a bad idea because if you call functions that also loop over the same global variable inside your loop, you'll end up with a lot of mess.
In my case changing the name of the variable helped, but the question is, why is it needed to circumvent the helpervariable when it is suposed to be in it's place. Is the scope of a variable 100%-correct under any circumstances?
I don't know any bugs here and I have written a lot of code. It's most likely a bug in your code, not in Hollywood.
I don't want to sound harsh or blame Hollywood/LUA, but this is not the first time I come across this "scopeproblem" and fixed it with just renaming the variable, but sooner or later I will run out of names for variables...and of cause the beauty of the sourcecode doesn't get better with this...

As I can't give a simple example were the variables are getting wrong I would like to ask If passing a variable via "Return(i)" can have an negative effect of how loops do work with the helpervariable?
Only if you're using a global helper variable. This can get you into problems very easily. So make sure you always use

For Local i = 0 To ...

Then you're on the safe side :)
Locked