Jump to content

Array of troubles


Recommended Posts

I am getting this error "Array variable has incorrect numbers of subscripts or subscript dimension range exceeded"

Could you tell me what causes this error?

Here is my source code:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon
Global $soundcount
Global $alarm
Global $alarm2


Global $clear[1]
Global $first[1]
Global $sell[1]
Global $sellprice[1]
Global $buy[1]
Global $buyprice[1]
Global $roundedbuy[1]
Global $roundedsell[1]

Global $first = $clear
Global $sell = $clear
Global $sellprice = $clear
Global $buy = $clear
Global $buyprice = $clear
Global $roundedbuy = $clear
Global $roundedsell = $clear










$sound = @WindowsDir & "\media\tada.wav"
$errcount = 0
#Region ### START Koda GUI section ### Form=C:\Users\techwg\Documents\btc.kxf
$Form1 = GUICreate("Bitcoin Exchange Watcher V 1.4.1.1", 320, 132)
$Label1 = GUICtrlCreateLabel("Bitcoin Exchange Rate", 45, 8, 218, 28)
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
$Label2 = GUICtrlCreateLabel("Selling at:", 175, 40, 68, 20)



GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$Label3 = GUICtrlCreateLabel("Buying at:", 16, 40, 69, 20)


GUICtrlSetFont(-1, 10, 800, 0, "Arial")
Global $selllabel = GUICtrlCreateLabel("0.00", 250, 40, 55, 28)



GUICtrlSetFont(-1, 16, 800, 0, "Arial")
Global $buylabel = GUICtrlCreateLabel("0.00", 88, 40, 55, 28)



GUICtrlSetFont(-1, 16, 800, 0, "Arial")

Global $sellalert = GUICtrlCreateInput("0", 262, 70, 49, 21)




Global $alerttoggle = GUICtrlCreateCheckbox("Audio Alerting", 5, 90, 97, 17)


GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_CHECKED)

$warn1 = GUICtrlCreateLabel("Sell Rate Alert:", 170, 70, 90, 17)



GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
Global $buyalert = GUICtrlCreateInput("0", 100, 70, 49, 21)




$warn2 = GUICtrlCreateLabel("Buy Rate Alert:", 8, 70, 90, 17)




GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")


Global $problem = GUICtrlCreateLabel("Problem connecting, Retrying...", 140, 95, 170, 20)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
GUICtrlSetState($problem, $GUI_HIDE)

$MenuItem1 = GUICtrlCreateMenu("Menu")
$MenuItem2 = GUICtrlCreateMenuItem("About", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Donate", $MenuItem1)


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While 1
    Global $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case 0
            Getrates()
            Sleep(1000)
            If $nMsg = -3 Then Exit
            If $nMsg = 9 Then about()

            Sleep(1000)
            If $nMsg = -3 Then Exit
            If $nMsg = 9 Then about()

            Sleep(1000)
            If $nMsg = -3 Then Exit
            If $nMsg = 9 Then about()


        Case $MenuItem2
            about()

        Case $MenuItem3
            ClipPut('1MTkF9ZTcXtgvQX245TwTL2Ko3NMvJHz6P')
            MsgBox(0, 'Donate', 'My bitcoin address has been coppied to your clipboard. You may send a donation there. Thank you!')
    EndSwitch
WEnd


Func about()
    MsgBox(0, 'Bitcoin Exchange Watcher', 'Created by Morthawt ( www.morthawt.com )' & @CRLF & 'Close this about screen to continue checking the rates.')
EndFunc   ;==>about





Func Getrates()


Global $first = $clear
Global $sell = $clear
Global $sellprice = $clear
Global $buy = $clear
Global $buyprice = $clear
Global $roundedbuy = $clear
Global $roundedsell = $clear



    $buyalertlevel = GUICtrlRead($buyalert)
    $sellalertlevel = GUICtrlRead($sellalert)
    If $nMsg = -3 Then Exit
    If $nMsg = 9 Then about()

    $file = @TempDir & '\BTCrates.txt'

    $get = InetGet('https://mtgox.com/code/data/ticker.php', $file, 1)
    If $get = 0 Then
        GUICtrlSetState($problem, $GUI_SHOW)
        Sleep(1000)
        $errcount = $errcount + 1
        If $errcount = 5 Then
            MsgBox(0, 'Error', 'There was an error downloading the exchange rates.')
            Exit
        EndIf
    EndIf

    If $get > 0 Then $errcount = 0
    $open = FileRead($file)
    FileDelete($file)

    If $nMsg = -3 Then Exit

    If $get <> 0 Then
        GUICtrlSetState($problem, $GUI_HIDE)
        $first = StringSplit($open, ',')

        $sell = StringSplit($first[5], ':')
        $sellprice = $sell[2]


        $buy = StringSplit($first[4], ':')
        $buyprice = $buy[2]


        $roundedbuy = Round($buyprice, 2)
        $roundedsell = Round($sellprice, 2)


        GUICtrlSetData($selllabel, '$' & $roundedsell)
        GUICtrlSetData($buylabel, '$' & $roundedbuy)
        $alarm = 0
        $alarm2 = 0

        If $buyalertlevel + $sellalertlevel <> 0 Then

            If $buyalertlevel <> 0 Then
                If $buyalertlevel >= $roundedbuy Then $alarm = 1
            EndIf


            If $sellalertlevel <> 0 Then
                If $sellalertlevel <= $roundedsell Then $alarm2 = 1
            EndIf




            If $alarm = 1 Then
                $audioallowed = GUICtrlRead($alerttoggle)
                If $audioallowed = 1 Then
                    Beep(500, 500)
                    Beep(740, 500)

                EndIf

            EndIf

            If $alarm2 = 1 Then
                $audioallowed = GUICtrlRead($alerttoggle)
                If $audioallowed = 1 Then
                    Beep(740, 500)
                    Beep(500, 500)

                EndIf

            EndIf



        EndIf




    EndIf



EndFunc   ;==>Getrates

It does not always seem to happen but is very frustrating. I have tried to "clear" the array thinking that would solve it but no change.

Any help would be appreciated.

Thank you.

Link to comment
Share on other sites

Hi Morthawt,

I'd suggest you to check your arrays before you access them via fixed indexes (e.g. $array[1]).

After you use stringsplit, you can either check for the @error <> 1 or the amount of elements > 1.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi Morthawt,

I'd suggest you to check your arrays before you access them via fixed indexes (e.g. $array[1]).

After you use stringsplit, you can either check for the @error <> 1 or the amount of elements > 1.

I am lost. Check them for what exactly? What is it that needs to be done in order for the error (crash) to not happen?
Link to comment
Share on other sites

Hi Morthawt,

your accessing the array elements with "fixed" ids:

Dim $array[3]
$array[0] = 2
$array[1] = 1
$array[2] = 2

$error = $array[3] ; This will give you a Subscript or Dimesion range exceeded

Example with "errorcheck":

Dim $array[3]
$array[0] = 2
$array[1] = 1
$array[2] = 2
If $array[0] > 2 Then
    $error = $array[3]
Else
    ;Throw an exception.
EndIf
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi Morthawt,

your accessing the array elements with "fixed" ids:

Dim $array[3]
$array[0] = 2
$array[1] = 1
$array[2] = 2

$error = $array[3] ; This will give you a Subscript or Dimesion range exceeded

Example with "errorcheck":

Dim $array[3]
$array[0] = 2
$array[1] = 1
$array[2] = 2
If $array[0] > 2 Then
    $error = $array[3]
Else
    ;Throw an exception.
EndIf

This is confusing me. So I need to put a line in there that says if a [x] exists (which is higher than expected) then do something else like ignore it?

Edit: I can't make sense of this. I am trying to understand this and translate it to what I am doing, but I cannot grasp what you mean at all.

Edited by Morthawt
Link to comment
Share on other sites

Well, if you're using the StringSplit functions as you do in your original script you assume to recieve a fixed number of items from the function. What happens when you do not recieve the expected amount? Your script will fail...

So my recommendation is to do a little error checking so your script will continue.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Well, if you're using the StringSplit functions as you do in your original script you assume to recieve a fixed number of items from the function. What happens when you do not recieve the expected amount? Your script will fail...

So my recommendation is to do a little error checking so your script will continue.

ok lets see if I understand this. Here is some sample data from the API i am using:

{"ticker":{"high":26.649,"low":13,"vol":134924,"buy":17.9224,"sell":17.9224,"last":17.9224}

I first split it up by , then split that up by : so I can get the number for what I need. If the api does not return the format such as that above, say something like this "{error},oh" yea or something then my script looking for a [4] or [5] will not find it and THAT is what makes it crash?

Link to comment
Share on other sites

yep. :huh2:

now you've got it!

So either do not use numbers for accessing array items (use loops) or do error checks. ;)

$first = StringSplit($open, ',')



        If $first[0] > 5 Then


            $sell = StringSplit($first[5], ':')
            $sellprice = $sell[2]


            $buy = StringSplit($first[4], ':')
            $buyprice = $buy[2]


            $roundedbuy = Round($buyprice, 2)
            $roundedsell = Round($sellprice, 2)

        EndIf

So this should work then correct? If the info has less than 6 array slots then it will just ignore tyring to do anything with it and go on with the script.

Link to comment
Share on other sites

Yes.

The easiest way to get the number of elements in an array is to use Ubound.

Well so far my addition is making it not crash "so far".

What do you mean Ubound? I am not an expert on Autoit so I am not familiar with a lot of these terms.

Link to comment
Share on other sites

Well so far my addition is making it not crash "so far".

What do you mean Ubound? I am not an expert on Autoit so I am not familiar with a lot of these terms.

Your code could also be written like this:

$first = StringSplit($open, ',')

If UBound($first) > 6 Then

    $sell = StringSplit($first[5], ':')
    $sellprice = $sell[2]

    $buy = StringSplit($first[4], ':')
    $buyprice = $buy[2]

    $roundedbuy = Round($buyprice, 2)
    $roundedsell = Round($sellprice, 2)

EndIf

If you set the flag to 2 when using StringSplit, then element [0] will not contain the number of entries, and the array will contain one less item. Ubound can be used to test the array size if it is not known.

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