Jump to content

Functions and nested arrays. I'm stuck.


pumpaij
 Share

Go to solution Solved by TheXman,

Recommended Posts

Hello,

there's no API for a certain GIS-application to draw title blocks (for plan-production) so I thought that I'd give it a go in Autoit.
So far so good but unfortunately I'm stuck now.

I've got an array that contains arrays with the X-Position, Y-Position, Width and Height of rectangles I want to draw,

I wrote a function called "DrawRectangles" that does the stuff I want to do for each entry in my $testdata-array.
 

; Data from a text-file. Can also be much larger in rows and columns
; [[XPos, YPos, width, height],[...]]
Local  $testdata[4][4] = [["00.0", "00.00", "21.00", "29.70"], ["00.05", "00.05", "20.00", "28.70"], ["0.05", "5", "20.00", "05.00"], ["10.00", "00.50", "03.00", "03.00"]]


; Call the function DrawRectangles() for every array in my $testdata-array.
For $i = 0 to UBound($testdata)-1
   DrawRectangles($testdata[0][$i])
Next


Func DrawRectangles($myvar)
    ; Draw a dummy-rectangle
    ; Open the preferences-window of this rectangle
    ; I ControlClick a form and would like to paste the XPos-value of the testdata-array
    ConsoleWrite("I would fill in the X-Position: " & $myvar & @CRLF)
    ; Some AutoIT code again
    ; I ControlClick a form and would like to paste the YPos-value of the testdata-array
    ConsoleWrite("I would fill in the Y-Position: " & $myvar & @CRLF)
    ; I ControlClick a form and would like to paste the width-value of the testdata-array
    ConsoleWrite("I would fill in the width: " & $myvar & @CRLF)
    ; I ControlClick a form and would like to paste the height-value of the testdata-array
    ConsoleWrite("I would fill in the height: " & $myvar & @CRLF)
    ; ControlClick the "OK" Button of the preferences window and repeat for the next array-entry
 EndFunc

In one function call, each one of the four values of the inner array should get pasted into the corresponding fields.

But right now the output is as follows:

I would fill in the X-Position: 00.0
I would fill in the Y-Position: 00.0
I would fill in the width: 00.0
I would fill in the height: 00.0
I would fill in the X-Position: 00.00
I would fill in the Y-Position: 00.00
I would fill in the width: 00.00
I would fill in the height: 00.00
I would fill in the X-Position: 21.00
I would fill in the Y-Position: 21.00
I would fill in the width: 21.00
I would fill in the height: 21.00
I would fill in the X-Position: 29.70
I would fill in the Y-Position: 29.70
I would fill in the width: 29.70
I would fill in the height: 29.70

And what I want is this:

I would fill in the X-Position: 00.0
I would fill in the Y-Position: 00.00
I would fill in the width: 21.00
I would fill in the height: 29.70
I would fill in the X-Position: 00.05
I would fill in the Y-Position: 00.05
I would fill in the width: 20.00
I would fill in the height: 28.70
I would fill in the X-Position: 0.05
I would fill in the Y-Position: 5
I would fill in the width: 20.00
I would fill in the height: 25.00
I would fill in the X-Position: 10.00
I would fill in the Y-Position: 00.50
I would fill in the width: 03.00
I would fill in the height: 03.00

I know it should be a simple loop-thing and I know that the script right now does exactly what the code says. The $myvar-value is the same four times so it gets inserted four times.
But no matter what I try, I can't get it to work. Playing around with the Function-Definition always results in errors ("Subscript used on non-accessible variable.:", "Badly formatted "Func" statement.:",...) and playing around with the For-Loop doesn't help me either.

 

If someone could nudge me in the right direction, it'd be highly appreciated.

Link to comment
Share on other sites

  • Solution

 

42 minutes ago, pumpaij said:

If someone could nudge me in the right direction, it'd be highly appreciated.

There a many ways that it could be done.  Here's a "nudge" that shows one of those ways:

; Data from a text-file. Can also be much larger in rows and columns
; [[XPos, YPos, width, height],[...]]
Global  $testdata[4][4] = [["00.0", "00.00", "21.00", "29.70"], ["00.05", "00.05", "20.00", "28.70"], ["0.05", "5", "20.00", "05.00"], ["10.00", "00.50", "03.00", "03.00"]]

; Call the function DrawRectangles() for every array in my $testdata-array.
For $i = 0 to UBound($testdata)-1
   DrawRectangles($testdata[$i][0], $testdata[$i][1], $testdata[$i][2], $testdata[$i][3])
Next

Func DrawRectangles($x, $y, $w, $h)
    ; Draw a dummy-rectangle
    ; Open the preferences-window of this rectangle
    ; I ControlClick a form and would like to paste the XPos-value of the testdata-array
    ConsoleWrite("I would fill in the X-Position: " & $x & @CRLF)
    ; Some AutoIT code again
    ; I ControlClick a form and would like to paste the YPos-value of the testdata-array
    ConsoleWrite("I would fill in the Y-Position: " & $y & @CRLF)
    ; I ControlClick a form and would like to paste the width-value of the testdata-array
    ConsoleWrite("I would fill in the width: " & $w & @CRLF)
    ; I ControlClick a form and would like to paste the height-value of the testdata-array
    ConsoleWrite("I would fill in the height: " & $h & @CRLF)
    ; ControlClick the "OK" Button of the preferences window and repeat for the next array-entry
EndFunc

 

Link to comment
Share on other sites

It is not an array embedding arrays (or if you prefer arrays of array).  It is just a 2D array (double dimension).  Either you need to initialize the array differently or use it as a 2D array.  Since there is always 4 coord for each rectangle, I would strongly recommend you keep your 2D array as it it.  Now to call a function to draw rect based on this array you could simply do :

; Data from a text-file. Can also be much larger in rows and columns
; [[XPos, YPos, width, height],[...]]
Local  $testdata[4][4] = [[00.0, 00.00, 21.00, 29.70], [00.05, 00.05, 20.00, 28.70], [0.05, 5, 20.00, 05.00], [10.00, 00.50, 03.00, 03.00]]


; Call the function DrawRectangles() for every array in my $testdata-array.
For $i = 0 to UBound($testdata)-1
   DrawRectangles($testdata, $i)
Next


Func DrawRectangles(ByRef $myvar, $iInd)
    ; Draw a dummy-rectangle
    ; Open the preferences-window of this rectangle
    ; I ControlClick a form and would like to paste the XPos-value of the testdata-array
    ConsoleWrite("I would fill in the X-Position: " & $myvar[$iInd][0] & @CRLF)
    ; Some AutoIT code again
    ; I ControlClick a form and would like to paste the YPos-value of the testdata-array
    ConsoleWrite("I would fill in the Y-Position: " & $myvar[$iInd][1] & @CRLF)
    ; I ControlClick a form and would like to paste the width-value of the testdata-array
    ConsoleWrite("I would fill in the width: " & $myvar[$iInd][2] & @CRLF)
    ; I ControlClick a form and would like to paste the height-value of the testdata-array
    ConsoleWrite("I would fill in the height: " & $myvar[$iInd][3] & @CRLF)
    ; ControlClick the "OK" Button of the preferences window and repeat for the next array-entry
 EndFunc

Notice I remove the double-quotes, since they are numbers not strings.  If you want to format a number use StringFormat...

Edited by Nine
corrected a small bug
Link to comment
Share on other sites

So that's how it's done. I feel stupid but I was trying all sorts of  $i-combinations and couldn't get it to work. Thank you for your quick responses - both methods work well!


I'm not sure about the difference between the array of arrays and the 2D-array.

Am I correct in visualizing it like this?

Array of Arrays:
        col1    col2    col3    col4    
row1      a       b       c      d  
row2      a       b       c      d
row3      a       b       c      d      
row4      a       b       c      d

2D-Array:
        col1      col2        col3        col4
row1 [a,b,c,d]  [a,b,c,d]   [a,b,c,d]   [a,b,c,d]

Thanks a lot again!

Link to comment
Share on other sites

1 hour ago, pumpaij said:

Am I correct in visualizing it like this?

No.  Take this simple code to describe an array of arrays :

Local $a[] = [1,2,3]    ; one array
Local $b[] = [1,2,3,4]  ; second array

Local $embed[] = [$a, $b]  ; now array of arrays

ConsoleWrite(($embed[1])[3] & @CRLF) ; you can use this approach to access subarrays
; but it will not work if you try to set things that way
; ($embed[1])[3] = 5

; Instead, you will need to do something like this
SetSub($embed[1], 3, 5)
ConsoleWrite(($embed[1])[3] & @CRLF)

Func SetSub(ByRef $array, $sub, $val)
  $array[$sub] = $val
EndFunc

 

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