BIM Coordinator Program (INT) April 22, 2024

Find the next step in your career as a Graphisoft Certified BIM Coordinator!

GDL
About building parametric objects with GDL.
SOLVED!

How to save an info from external text to a parameter?

A_ Smith
Expert
AC22
I was trying to save text (numbers in text files are string type) to parameter. I've created parameter "x" - text array. Didn't change it, so x[1] = ""

Master script

dim x[5][5]
for i=1 to 5
	for j=1 to 5
		x=""
	next j
next i


ch=OPEN("text",”voda.txt”,"separator='\t', mode=ro, Library")
	iLine=1
	nr=INPUT(ch, iLine, 1, v1, v2, v3, v4, v5)
	WHILE nr>-1 DO	!just to limit, original text filex has >50 rows
			if nr then
				x[iLine][1] = v1
				x[iLine][2] = v2	
				x[iLine][3] = v3	
				x[iLine][4] = v4	
				x[iLine][5] = v5
			else
				add2 0, -1	!empty row
			endif
			iLine=iLine+1
			nr=INPUT(ch, iLine, 1, v1, v2, v3, v4, v5)
	ENDWHILE
CLOSE ch

for iLine=1 to 5
	for j=1 to 5
		text2 5*(j-1),1-iLine,x[iLine]
	next j
next iLine

x[1][1]="a b c"	!without it X_array stays empty; with it x[1][1] is overwritten and first column and row stays empty, BUT rest values are correct. why ?!!
parameters x=x
but idk why "parameters x=x" has no effect. I mean when I check "x" parameter it remains empty (if comment x[1][1]='abc'). Because after closing reading the file text2 cycles show proper values in 2D view (without it I'd think that code is wrong and it even didn't read the file). So array should have correct values, I guess.

----->”voda.txt”<---------
0	4	6	8	10
1	0,22	0,28	0,33	0,39
2	0,23	0,29	0,34	0,40
3	0,25	0,30	0,36	0,42
4	0,26	0,32	0,38	0,44
AC 22, 24 | Win 10
10 REPLIES 10
Peter Baksa
Graphisoft
Graphisoft
It is a quite complex chain of GDL quirks that leads to this behaviour.

The text add-on uses a fixed logic to determine the type of the variables it sets, disregarding what it was initialized to. If it looks like a number it is a number, otherwise a string. Decimal separators are . not , So in your file some entries will be read as numbers, some as strings.

GDL array variables can hold mixed types (integer and string), but parameter arrays can't.
It is quite possible to use a variable with the same name as a parameter. By writing dim x[][] you define a variable that shadows the parameter, not redefine the parameter.
Using parameters x = x the variable x with mixed types is assigned to the parameter x.

An error is given if the type of variable x doesn't match the parameter's type. When you are initializing x[1][1] with "a b c" you match the types.
The type check tries to make a general check independent of the parameters and input files, so it reports an error even if the current state of the file and the parameters would run without error. But it doesn't run the code, so the actual input doesn't happen, and it doesn't complain about the type mismatch at x[1][2].

The missing array elements are missing because they don't match the type, and they aren't converted like in some other languages.
Péter Baksa
Software Engineer, Library as a Platform
Graphisoft SE, Budapest
Learn and get certified!