cancel
Showing results for 
Search instead for 
Did you mean: 
EN
cancel
Showing results for 
Search instead for 
Did you mean: 
Nader Belal
Mentor

Question about Arrays & Subroutines

Hi there,

I would like to ask why the creation of arrays in a sub-routine in Parameter script as in the following example is generating an error ("Incompatible array dimensions")


some script

if condition is met then
	gosub 001

	_tempArray = _origArray		!<--both arrays are one dimension
endif

some script

end

001:

dim _tempArray

return

A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
1 Solution

Accepted Solutions
Jochen Suehlo
Moderator Emeritus
In my experience it happens, that arrays defined in a subroutine where not recognized in parts of scripts that are above the subroutine (in the text order).
Example:
gosub 100
_newVALUE = _tempArray[1]		
end
100:
dim _tempArray[]
_tempArray[1] = 2
return
But if you change this into this, it works:
gosub 100
gosub 200
end
100:
dim _tempArray[]
_tempArray[1] = 2
return

200:
_newVALUE = _tempArray[1]	
return
This is a weird thing, but I think, it is a bug.
Jochen Suehlo . AC12-29 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de

Go to post

4 Replies 4
Jochen Suehlo
Moderator Emeritus
In my experience it happens, that arrays defined in a subroutine where not recognized in parts of scripts that are above the subroutine (in the text order).
Example:
gosub 100
_newVALUE = _tempArray[1]		
end
100:
dim _tempArray[]
_tempArray[1] = 2
return
But if you change this into this, it works:
gosub 100
gosub 200
end
100:
dim _tempArray[]
_tempArray[1] = 2
return

200:
_newVALUE = _tempArray[1]	
return
This is a weird thing, but I think, it is a bug.
Jochen Suehlo . AC12-29 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de
Nader Belal
Mentor
@Joachim Suehlo

Thank you a lot.
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
leceta
Expert
From the book GDL Handbook, by Andrew Watson, 3.5.Scope, pages 36-37

Array variables have some interesting characteristics when it comes to scope. You must define an array before you can use it, which stands to reason. You can define an array variable in a sub-routine (we’ll discuss sub-routines later) but it’s best to define arrays in the main script. If you define an array variable in a sub-routine and then try to use it in the main script, error messages will pop up when you try to save the object.

and later:

The variable must be declared at a point in the script that comes both physically and logically ahead of the point at which it is used.
Peter Baksa
Graphisoft
Graphisoft
Joachim wrote:
This is a weird thing, but I think, it is a bug.
This isn't a bug, it's a limitation. The order of statements can depend on parameters, it is only known at run-time. Variables mustn't change their types during run, the best the interpreter can do is check the code line-by-line (there are simple cases when the order of execution is evident, but it can get complicated very easily). If it didn't do this check, it would be possible to get error messages only by some parameter states, which would make them very hard to detect and understand.
Péter Baksa
Software Engineer, Library
Graphisoft SE, Budapest

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!