Problems with arrays

Recommended Posts

This is a portion of my program

```\$distance[\$iconNumber]
\$distance2[\$iconNumber]
For \$i=0 To UBound(\$iconPos) -1
\$distance[\$i]=(((\$iconPos[\$i][0]-\$x)*(\$iconPos[\$i][0]-\$x))+((\$iconPos[\$i][1]-\$y)*(\$iconPos[\$i][1]-\$y)))^(1/2)
Next
For \$i=0 To UBound(\$distance) -1
If \$distance[\$i]<=100 Then
\$distance2[\$i]=\$distance[\$i]
EndIf
Next
If (UBound(\$distance2) -1) = 1 Then
\$distance2[0]=\$finalDistance
MsgBox(64,"Icon","Yeah")
EndIf```

It kept saying Syntax error on \$distance[\$iconNumber] and \$distance2[\$iconNumber], but I don't see a problem...

I already declared the variable on the top of the program.

Edited by rliiack

My Projects:Smart Icons

Share on other sites

Post a working reproduction of your problem. There's really not enough there to parse.

Also, use Global or Local to declare your arrays. Use whichever keyword is appropriate to the intended scope of the variable.

Share on other sites

I solved the problem! Thank you anyways.

My Projects:Smart Icons

Share on other sites

I have another question... What does _ArraySearch return?

I know it says "The index that \$vValue was found at" but when I put

```\$minIndexPos=_ArraySearch(\$finalDistance,_ArrayMinIndex(\$finalDistance))
\$finalDistance[\$minIndexPos][1]][0]```

it says Array variable subscript badly formatted.:

\$finalDistance[^ ERROR

My Projects:Smart Icons

Share on other sites

I have another question... What does _ArraySearch return?

I know it says "The index that \$vValue was found at" but when I put

```\$minIndexPos=_ArraySearch(\$finalDistance,_ArrayMinIndex(\$finalDistance))
\$finalDistance[\$minIndexPos][1]][0]```

it says Array variable subscript badly formatted.:

\$finalDistance[^ ERROR

And yet again no information on what those arrays looked like before or what you are trying to do? If \$finalDistance is a 3D array then _ArraySearch() can't use it. If it's not a 3D array then why do you have three indexes plus a stray close-bracket?

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share on other sites

Sorry because I thought the program would be too tedious to look at...

```#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
Global \$finalDistance, \$msg, \$desktop, \$iconNumber, \$x, \$y, \$pos, \$iconPos, \$counter, \$minIndex, \$minIndexPos

Global \$zoomHeight=200
Global \$zoonWidth=200

opt("GUIOnEventMode",1)
\$SI = GUICreate("Smart Icons", 625, 445, 192, 124,\$WS_MINIMIZEBOX)
GUISetFont(20, 800, 0, "Australian Sunrise")
\$Group1 = GUICtrlCreateGroup("Sound Options", 168, 32, 297, 105)
\$Slider1 = GUICtrlCreateSlider(184, 80, 193, 49)
\$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 384, 96, 17, 17)
\$Label1 = GUICtrlCreateLabel("Mute", 400, 96, 32, 19)
GUICtrlSetFont(-1, 10, 800, 0, "Times New Roman")

\$Group2 = GUICtrlCreateGroup("Magnifying Size", 168, 168, 297, 105)
GUICtrlCreateInput("", 216, 208, 97, 32)
\$Label2 = GUICtrlCreateLabel("X", 320, 224, 13, 19)
GUICtrlSetFont(-1, 10, 800, 0, "Times New Roman")

\$Quit = GUICtrlCreateButton("Quit", 240, 304, 145, 57, \$WS_GROUP)
GUICtrlSetFont(-1, 28, 800, 0, "Australian Sunrise")
GUICtrlSetOnEvent(\$Quit, "Quit")

GUISetState(@SW_SHOW)

_GDIPlus_Startup()

While 1

getMousePosition()
getIconPositions()

Global \$distance[\$iconNumber]
Global \$distance2[\$iconNumber]
For \$i=0 To UBound(\$iconPos) -1
\$distance[\$i]=(((\$iconPos[\$i][0]-\$x)*(\$iconPos[\$i][0]-\$x))+((\$iconPos[\$i][1]-\$y)*(\$iconPos[\$i][1]-\$y)))^(1/2)
Next
For \$i=0 To UBound(\$distance) -1
If  \$distance[\$i]<=100 Then
\$distance2[\$i]=\$distance[\$i]
EndIf
Next
Local \$hit=_GUICtrlListView_HitTest(\$desktop,-1,-1)
accessArrayData()
If (\$finalDistance[\$minIndexPos][0]<100 And \$finalDistance[\$minIndexPos][0]>0) Or \$hit[2]=True Then

opt("GUIOnEventMode",1)
\$hHandle=GUICreate("Smart Icon",200,200,\$x+30,\$y, BitOR(\$WS_POPUP, \$WS_BORDER, \$WS_DISABLED), BitOR(\$WS_EX_TOPMOST, \$WS_EX_TOOLWINDOW))
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent(\$Quit, "Quit")

\$graphics=_GDIPlus_GraphicsCreateFromHWND(\$hHandle)
\$hbitmap=_ScreenCapture_Capture("",\$iconPos[\$finalDistance[\$minIndexPos][1]][0],\$iconPos[\$finalDistance[\$minIndexPos][1]][1],\$iconPos[\$finalDistance[\$minIndexPos][1]][0]+76,\$iconPos[\$finalDistance[\$minIndexPos][1]][1]+69,false)
\$bitmap=_GDIPlus_BitmapCreateFromHBITMAP(\$hbitmap)
\$height=_GDIPlus_ImageGetHeight(\$bitmap)
\$width=_GDIPlus_ImageGetWidth(\$bitmap)
_GDIPlus_GraphicsDrawImageRectRect(\$graphics,\$bitmap,0,0,\$height,\$width,0,0,\$zoonWidth,\$zoomHeight)
_GDIPlus_GraphicsDispose(\$graphics)
_WinAPI_DeleteObject(\$hbitmap)
_WinAPI_DeleteObject(\$bitmap)
sleep(25)
GUIDelete(\$hHandle)
EndIf
WEnd

Func getMousePosition()
\$pos=MouseGetPos()
\$x=\$pos[0]
\$y=\$pos[1]
EndFunc

Func getIconPositions()
\$desktop=ControlGetHandle("[CLASS:Progman]","","[CLASS:SysListView32;INSTANCE:1]")
\$iconNumber=_GUICtrlListView_GetItemCount(\$desktop)
If \$iconNumber>0 Then
Global \$iconPos[\$iconNumber][2]
For \$i=0 To UBound(\$iconPos) -1
\$iconPos[\$i][0]=_GUICtrlListView_GetItemPositionX(\$desktop, \$i)
\$iconPos[\$i][1]=_GUICtrlListView_GetItemPositionY(\$desktop, \$i)
Next
Else
MsgBox(64,"Error","No icons on desktop.", 3)
EndIf

;    _ArrayDisplay(\$iconPos, "Icon X Position - Icon Y Position")
EndFunc

Func Quit()
Exit
EndFunc

Func accessArrayData()
\$counter=0
Global \$finalDistance[UBound(\$distance2)][2]
for \$i=0 To UBound(\$distance2) -1
If \$distance2[\$i]>0 Then
\$finalDistance[\$counter][0]=\$distance2[\$i]
\$finalDistance[\$counter][1]=\$i
\$counter=\$counter+1
EndIf
Next
\$minIndexPos=_ArraySearch(\$finalDistance,_ArrayMinIndex(\$finalDistance))
EndFunc```

Errors:

SmartIcons.au3 (80) : ==> Array variable subscript badly formatted.:

\$hbitmap=_ScreenCapture_Capture("",\$iconPos[\$finalDistance[\$minIndexPos][1]][0],\$iconPos[\$finalDistance[\$minIndexPos][1]][1],\$iconPos[\$finalDistance[\$minIndexPos][1]][0]+76,\$iconPos[\$finalDistance[\$minIndexPos][1]][1]+69,false)

\$hbitmap=_ScreenCapture_Capture("",\$iconPos[\$finalDistance[^ ERROR

\$finalDistance is a 2D array

Edited by rliiack

My Projects:Smart Icons

Share on other sites

Confusing mess like this should be broken down into parts:

`\$hbitmap = _ScreenCapture_Capture("", \$iconPos[\$finalDistance[\$minIndexPos][1]][0], \$iconPos[\$finalDistance[\$minIndexPos][1]][1], \$iconPos[\$finalDistance[\$minIndexPos][1]][0] + 76, \$iconPos[\$finalDistance[\$minIndexPos][1]][1] + 69, False)`

It's not that it can't work, just that confusing syntax leads to errors that can be very hard to see/fix.

This way creates intermediate variables (which can be output in debug monitors):

```; \$minIndexPos is an integer
; \$finalDistance is a 2D array
; \$iconPos is a 2D array
\$iFD = \$finalDistance[\$minIndexPos][1]
\$iSC_X1 = \$iconPos[\$iFD][0]
\$iSC_Y1 = \$iconPos[\$iFD][1]
\$iSC_X2 = \$iconPos[\$iFD][0] + 76
\$iSC_Y2 = \$iconPos[\$iFD][1] + 69
\$hbitmap = _ScreenCapture_Capture("", \$iSC_X1, \$iSC_Y1, \$iSC_X2, \$iSC_Y2, False)```

Or, you could just break up the line:

```\$hbitmap=_ScreenCapture_Capture("", _
\$iconPos[\$finalDistance[\$minIndexPos][1]][0], _
\$iconPos[\$finalDistance[\$minIndexPos][1]][1], _
\$iconPos[\$finalDistance[\$minIndexPos][1]][0] + 76, _
\$iconPos[\$finalDistance[\$minIndexPos][1]][1] + 69, _
False)```

Either way it's easier to read and debug. The syntax doesn't look wrong but you need to test the inputs to those array references:

- Is \$minIndexPos a valid integer?

- Is \$finalDistance a 2D array, and is \$finalDistance[\$minIndexPos][1] a valid element at run time?

- Is \$iconPos a 2D array, and are the [n][0] and [n][1] references to valid elements at run time?

Add some ConsoleWrite(), or _DebugOut(), or something, to show the values of the inputs to the function before _ScreenCapture_Capture().

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share on other sites

Thanks for the advice, I think I got it now.

My Projects:Smart Icons

Create an account

Register a new account