Sign in to follow this  
Followers 0
Nevin

Array variable problem

10 posts in this topic

I'm having a very strange problem.

When I use a variable for array elements, I get an error. If I use a plain old value, it works fine. Look below on the line that says ;PROBLEM AREA

That is generating the error. If I replace the two $count variables with zero (which is what count is equal to), the error goes away.

Am I going nuts, or what? My workaround was just putting the number in..the only problem is..this is was in a loop, so I had to write more code.

So, to repeat myself briefly:

Using $count in the "PROBLEM AREA" generates an error. Change where it says $count to 0, and it works fine. WTFBBQ.

#include <Array.au3>

;takes in an 8-bit binary number and returns the decimal equivalent.
;Note: If used for raw values, be sure to pass inside quotations so that leftmost zeroes are not truncated
Func BinToDec($Input)
    
    Dim $Output = ""
    
    $num = stringsplit($Input,"")
    _arraydelete($num,0)
    
    $base = 128 ;value of the bit the loop starts on.
    $count = 0
    while $count < 8
        If $num[$count] = 1 Then
            $Output += $base ;if the bit in question is a 1, it adds $base
        EndIf
                
        $base /= 2 ;divides base in half for the next bit
        $count += 1 ;moves onto the next bit
    WEnd
    
    ;if $Input was all zeroes, sets $Input to 0
    If $Output = "" Then
        $Output = 0
    EndIf
    
    Return ($Output)
    
EndFunc

Dim $ArrayOfBinaryNums[8] = ["10000000","11000000","11000000","11111111","00000000","00000000","00000000","00000000"]
Dim $DecimalVersion[8]

$count = 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$DecimalVersion[$count] = BinToDec($ArrayOfBinaryNums[$count]) ;PROBLEM AREA
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

By the way, all the function does is just convert those binary numbers to decimal. I couldn't find a predefined function so I made my own ghetto version.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The "binary number" you are passing is a text string. So be careful with typing inside your function:

#include <Array.au3> ; Only for _ArrayDisplay()

;takes in an 8-bit binary number and returns the decimal equivalent.
;Note: If used for raw values, be sure to pass inside quotations so that leftmost zeroes are not truncated
Func BinToDec($Input)
   
    Local $Output = 0
   
    $num = stringsplit($Input,"")
    If $num[0] <> 8 Then
        MsgBox(16, "Error", "Invalid input to function: " & $Input)
        Return SetError(1, 0, 0)
    EndIf

    For $n = 1 To $num[0]
        If $num[$n] = "1" Then $Output += (2 ^ (8 - $n))
    Next    

    Return $Output
EndFunc

Dim $ArrayOfBinaryNums[8] = ["10000000","11000000","11000000","11111111","00000000","00000000","00000000","00000000"]
Dim $DecimalVersion[Ubound($ArrayOfBinaryNums)]

For $count = 0 To Ubound($ArrayOfBinaryNums) - 1
    $DecimalVersion[$count] = BinToDec($ArrayOfBinaryNums[$count])
Next

_ArrayDisplay($DecimalVersion, "Results: $DecimalVersion")

Not tested, 'cause I'm on a Mac right now.

:D

Edit: Tweaked bug per next post.

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 this post


Link to post
Share on other sites

#3 ·  Posted (edited)

:D Same error:

C:\Documents and Settings\Nevin Festger\Desktop\test.au3 (26) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$DecimalVersion[$count] = BinToDec($ArrayOfBinaryNums[$count])

^ ERROR

Fixed the same way, too. I see you changed things around a bit, I'll take a look at it later. Looks a bit smaller :D

Edited by Nevin

Share this post


Link to post
Share on other sites

:D Same error:

C:\Documents and Settings\Nevin Festger\Desktop\test.au3 (26) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$DecimalVersion[$count] = BinToDec($ArrayOfBinaryNums[$count])

^ ERROR

Fixed the same way, too. I see you changed things around a bit, I'll take a look at it later. Looks a bit smaller :)

Oops... :D

Just take the "- 1" off the declaration of the $DecimalVersion array so it will be the same size. Should be:

Dim $DecimalVersion[Ubound($ArrayOfBinaryNums)]

:D


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 this post


Link to post
Share on other sites

What are you talking about? There is no -1 on the declaration, that's on the loop counter. :D

The reason it is giving that error is the same wacky bug that I get on my version. Since you don't think it's strange, I am thinking it may be some kind of bug in the language.

Share this post


Link to post
Share on other sites

What are you talking about? There is no -1 on the declaration, that's on the loop counter. :D

The reason it is giving that error is the same wacky bug that I get on my version. Since you don't think it's strange, I am thinking it may be some kind of bug in the language.

No. I had it wrong, was getting your error, and fixed it. I also tested it, and the fix makes it work fine. The broken declaration looked like this:

Dim $DecimalVersion[ubound($ArrayOfBinaryNums) - 1]

The fixed version looks like this:

Dim $DecimalVersion[ubound($ArrayOfBinaryNums)]

Note the "Dim" key word, which is not part of a loop control.

:D


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 this post


Link to post
Share on other sites

Ohhhh, didn't notice you edited the old post. Gotcha. Thanks for the working version.

So, why didn't mine work? :D

Share this post


Link to post
Share on other sites

Ohhhh, didn't notice you edited the old post. Gotcha. Thanks for the working version.

So, why didn't mine work? :D

You were mixing text string and numeric variables, and trying to do math with both.

:D


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 this post


Link to post
Share on other sites

Huh? My function worked. The problem was that I couldn't use $count instead of 0 for the array subscript.....

Share this post


Link to post
Share on other sites

Mwahahaha! My unexplainable problem has defeated you all!...

That's alright. I'll just chaulk it up on my bug list.

chaulk? chalk.....?

...

*runs away*

Share this post


Link to post
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
Sign in to follow this  
Followers 0