Jump to content

function call in a wrong place?


imppi
 Share

Recommended Posts

Hi

something not right in the code. whats the problem there with the inner while-loop?

global $num_of_elements = 35
global $shuffled_array[$num_of_elements]
global $orig_array[$num_of_elements] = [430, 428, 427, 427, 430, 432, 435,   438, 439, 439, 441, 437, 440, 440,  434, 438, 436, 433, 435, 440, 430,  444, 443, 444, 443, 446, 447, 448, 427, 426, 426, 426, 425, 424, 430]
global $j=0
global $i=0
shuffle_array()

func shuffle_array()
    For $k = 0 To $num_of_elements -1 Step 1
        $shuffled_array[$k] = 0
    next
    while $i < $num_of_elements
        while $shuffled_array[  $j=random(0, $num_of_elements-1, 1)  ] == 0
            $shuffled_array[$j]=$orig_array[$i]
            $i += 1
        WEnd
    wend
EndFunc
Link to comment
Share on other sites

You can't do variable assignment inside the array index field: "$shuffled_array[ $j = Random(...) ]"

:)

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
Link to comment
Share on other sites

got no warning or error. it just didn't work as i thought.

You don't get an error because it's valid syntax, just not what you wanted. In a place in the script line where assignment is NOT valid (not at the begining of the line) it does comparison with the "=" operator. So "$j = random(0, $num_of_elements-1, 1)" returns True or False for the compare, it doesn't change the value to $j.

Of course a Boolean type is not a valid array index, but in most cases native AutoIt functions will re-type input variables as required, so the Boolean becomes either 0 (False) or 1 (True) and that's your index. No errors, but your array index will always be 0 or 1 based on the Boolean compare operation.

Demo:

Global $aArray[3] = ["A", "B", "C"]
Global $n = 1
ConsoleWrite("Result = " & $n & @CRLF)
ConsoleWrite("Result = ")
ConsoleWrite($n = 0)
ConsoleWrite(@CRLF)
ConsoleWrite("Result = ")
ConsoleWrite($n = 1)
ConsoleWrite(@CRLF)
ConsoleWrite("Result = " & $aArray[$n = 0] & @CRLF)
ConsoleWrite("Result = " & $aArray[$n = 1] & @CRLF)
ConsoleWrite("Result = " & $aArray[$n = 2] & @CRLF)

:)

Edit: Typo

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
Link to comment
Share on other sites

Global $array[10], $Elements = 9
;before shuffling
For $i = 0 to 9 Step 1
    $array[$i] = $i
    ConsoleWrite($array[$i])
Next
ConsoleWrite(@CRLF)

Shuffle($Elements)

;after shuffling
For $i = 0 to 9 Step 1
    ;$array[$i] = $i
    ConsoleWrite($array[$i])
Next
ConsoleWrite(@CRLF)
Func Shuffle($Elements)
    For $i = 0 To $Elements*2 Step 1
        $index1 = Random(0, $Elements, 1)
        $index2 = Random(0, $Elements, 1)
        $temp = $array[$index1]  ;instead of including Array.au3 and using _ArraySwap
        $array[$index1] = $array[$index2]
        $array[$index2] = $temp
        Next
EndFunc

_____________________________________________________________________________

Link to comment
Share on other sites

In example listed above and _ArraySwap() used a $temp variable, but does know any one how to swap them WITHOUT creating temporary $var?

(It's possible and works faster, but i forgot how).

Edited by Godless

_____________________________________________________________________________

Link to comment
Share on other sites

In example listed above and _ArraySwap() used a $temp variable, but does know any one how to swap them WITHOUT creating temporary $var?

(It's possible and works faster, but i forgot how).

Don't be daft.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

IF $I'm_daft = TRUE then Show me temporary var:

Global $x = "20", $y = "314"

$x = BitXOR($x, $y)
$y = BitXOR($y, $x)
$x = BitXOR($x, $y)

MsgBox(0,$x & " " & $y, "")

Ans where is your proof that is faster?
Link to comment
Share on other sites

#include <Timers.au3>

Global $x = "20", $y = "314", $starttime

$starttime = _Timer_Init()
For $i = 0 To 10000 Step 1
$z = $x
$x = $y
$y = $z
Next
ConsoleWrite(_Timer_Diff($starttime)& @CRLF)

$starttime = _Timer_Init()
For $i = 0 To 10000 Step 1
$x = BitXOR($x, $y)
$y = BitXOR($y, $x)
$x = BitXOR($x, $y)
Next
ConsoleWrite(_Timer_Diff($starttime)& @CRLF)

_____________________________________________________________________________

Link to comment
Share on other sites

That was interesting.... Or maybe not.

Even after fixing that flaud test your BitXOR method was only HALF as fast as copying to a variable. :)

Global $x = "20", $y = "314", $starttime, $i

For $i = 0 To 10000000      ;Should be enough to trick Cool'n'Quiet, SpeedStep etc on most computers.
Next

$starttime = TimerInit()
For $i = 0 To 10000 Step 1
$z = $x
$x = $y
$y = $z
Next
$endtime1 = TimerDiff($starttime)

$starttime = TimerInit()
For $i = 0 To 10000 Step 1
$x = BitXOR($x, $y)
$y = BitXOR($y, $x)
$x = BitXOR($x, $y)
Next
$endtime2 = TimerDiff($starttime)

ConsoleWrite($endtime1 & "/" & $endtime2 & @CRLF)   ;Not sure if this was necessary

I bet that if we did this test on an array as the topic was on your method would be even slower, since working on arrays are far slower than working on variables. B)

;)

Edit: I got around 16 ms vs 32 ms

Edit2: Clarification

Edited by AdmiralAlkex
Link to comment
Share on other sites

IF $I'm_daft = TRUE then Show me temporary var:

Global $x = "20", $y = "314"

$x = BitXOR($x, $y)
$y = BitXOR($y, $x)
$x = BitXOR($x, $y)

MsgBox(0,$x & " " & $y, "")

Well that doesn't use an extra variable so I must hastily admit defeat. However it does create an extra value but uses an existing variable so I'm going to treat it as a technical defeat but not a moral one. Not that I'm a sore looser or anything but it's just that I'm a, um, .. a sore looser. :)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...