Jump to content
Sign in to follow this  
jaberwacky

My latest bugged out idea...

Recommended Posts

jaberwacky

I had this idea for people who want to assign an unknown amount of variables but whom do not wish to either declare an oversized array or to redim an array. I think it's pretty neat, however, a more experienced programmer may see the flaws a mile off.

I used the 'Assign()' and 'Eval()' functions. 'new()' wraps around 'Assign()' with some error checking built-in.

'retrieve()' will get the data from the specified variable with some extra error checking.

I kinda see this as a distant cousin (named Cletus) to the 'new' operator in some languages and to linked lists.

Edit (May-05-2010): Now you can sort the variables using an extremely naive bubblesort. The code that follows is only proof of concept! So, I'll ask that ye may laugh quietly and I'll thank ye kin'ly (kidding).

Ok, now for a better explanation of this script. This uses Assign() to assign to an unknown amount of variables. First, create a loop with $index as your loop variable. Inside the loop use Assign() to assign a value to a variable named "$variable" & $index. This way you can "index" the variables.

Unlike an array you can't simply set this to zero. So unless you declare your variables within a function, then these variables will take up memory throughout the program's lifetime.

This script has usefulness in that you can structure the data as shown by the bubble sort. As far as actually being usefull well, who knows. I've never had the need to declare an array of zero elements. To me, an array of zero elements isn't an array. Either that or redim is still being used behind the scenes.

Global Const $name = "$variable"
Global Const $limit = Random(30, 669, 1)
Global $data, $index = 0
Global $array[$limit]

; loads random data into a random amount of variables
For $index = 0 To $limit
    $data = Round(Random(0, 100), 2)
    new($index)
Next

; naive bubblesort
_bubbaSort()

; display the results of the bubblesort
$index = 0
While IsDeclared($name & $index)
    ConsoleWrite($name & $index & " " & retrieve($index) & @CRLF)
    $index += 1
WEnd

Exit

Func _bubbaSort() ; I bubbalieve! =D YAY!
    Local $a, $b

    For $index = $limit To 0 Step -1
        For $j = 1 To $index
            $a = retrieve($j)
            $b = retrieve($j - 1)

            If $a > $b Then ; two variable swap
                $a -= $b
                $b += $a
                $a = ($b - $a)

                Assign($name & $j, $a)
                Assign($name & $j - 1, $b)
            EndIf
        Next
    Next
EndFunc ;==>bubbaSort

Func new($index, $scope = 2)
    If $scope >= 0 And $scope <= 2 Then
        Local $return = Assign($name & $index, $data, $scope)
        If $return = 0 Then MsgBox(0, "$return", $return)
        If Not IsDeclared($name & $index) Then Return SetError(1)
    Else
        Return SetError(2); scope is out-of-bounds
    EndIf

EndFunc ;==>new

Func retrieve($index)
    Local $return = Eval($name & $index)

    If $return = '' And @error <> 0 Then
        Return SetError(1)
    EndIf
    Return $return
EndFunc ;==>retrieve
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
MHz

I consider for idea. It may have a special use.

As for syntax, this seems like a concern.

If $scope <> 0 Or $scope <> 1 Or $scope <> 2 Or $scope <> 3 Or $scope = 4 Then

The 1st test is anything except 1. The next test is anything except 2. Now, the 2nd test is going to be true? If you test $scope for being 1 or 2 or 3 or 4 then all would pass?

Share this post


Link to post
Share on other sites
jaberwacky

I consider for idea. It may have a special use.

As for syntax, this seems like a concern.

If $scope <> 0 Or $scope <> 1 Or $scope <> 2 Or $scope <> 3 Or $scope = 4 Then

The 1st test is anything except 1. The next test is anything except 2. Now, the 2nd test is going to be true? If you test $scope for being 1 or 2 or 3 or 4 then all would pass?

Hi Mhz. Thank you for responding to my post! I took your advice into consideration and manifested a change in the source code.

I once asked a question on this forum. I asked if there were such a function that would create a variable. Someone said that it would be a stupid idea. Then I found Assign(). So somebody behind the scenes of AutoIt doesn't think it would be stupid.

Share this post


Link to post
Share on other sites
MHz

Hi Mhz. Thank you for responding to my post! I took your advice into consideration and manifested a change in the source code.

I responded for my pleasure, not just yours :idea: . That line still seems to be incorrect. I consider that And would suit the task better.

; Both 1st test And 2nd test for 0,1,2  Or  3rd test = 4
If $scope >= 0 And $scope <= 2 Or $scope = 4 Then

Hmm, the 4 option seems new to me with Assign() though a while since I may have looked at it.

I once asked a question on this forum. I asked if there were such a function that would create a variable. Someone said that it would be a stupid idea. Then I found Assign(). So somebody behind the scenes of AutoIt doesn't think it would be stupid.

Some see only by what little they know and sometimes that can be little if they form an opinion on just that. Assign() etc have their special place and can do magic if used well at a suitable time. Your learning to explore the options which is better than not knowing and just saying it is stupid, right? :) Edited by MHz

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.