Followers 0

Re-ordering a simple array

6 posts in this topic

Hello everyone,

I created this small script that shows a list of 10 people in an array.

Each person is associated with a number which is stored in another array.

Chris is associated with 3, Brian with 4, Daniel with 2, Stephen with 6 and so on.

I then made the first array (with the names in them), to be sorted in alphabetical order.

How can I make the second array with the numbers in them, also sorted accordingly so that each person is still matched with their number?

```#include <Array.au3>
#include <File.au3>

Local \$Array1[10] = ["Chris","Brian","Daniel","Stephen","William", "Joseph", "Diana", "Thomas", "Jason", "Carol"]
Local \$Array2[10] = [3, 4, 2, 6, 7, 1, 9, 10, 5, 8]

_ArraySort(\$Array1)
_ArrayDisplay(\$Array1, "Sorted in Alphabetical Order")

;Display Array2 showing the new sorted out rows, so that each person is still matched with the number they are associated with. Results should show 4, 8, 3, 2, 9, 5, 1, 6, 10, 7```

Thanks,

Brian

Share on other sites

Use a 2d array instead of 2x 1d arrays.

Br,

UEZ

1 person likes this

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share on other sites

Like UEZ said, like this:

```#include <Array.au3>

Local \$Array1 = [ _
["Chris", 3], _
["Brian", 4], _
["Daniel", 2], _
["Stephen", 6], _
["William", 7], _
["Joseph", 1], _
["Diana", 9], _
["Thomas", 10], _
["Jason", 5], _
["Carol", 8] _
]

_ArraySort(\$Array1)
_ArrayDisplay(\$Array1, "Sorted in Alphabetical Order")```
1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share on other sites

Thanks UEZ and jchd! That made sense and I was able to get the results I wanted thanks to the 2d array method.

I have one more question though,

jchd, you had to manually type out both of my columns in your code.

Is there a way to combine both of my original 1d arrays into a 2d array with 2 columns through some sort of autoit code? Because my arrays may have 1000 entries in each and it would take too long to manually type those out.

Thanks,

Brian

Share on other sites

Of course there is the simple pedestrian way: use a For..Next loop with a single index used to read \$Partial1DArray1, \$Partial1DArray2 and write \$Monster2DArray elements.

```#include <Array.au3>

Local \$Array1 = ["Chris","Brian","Daniel","Stephen","William", "Joseph", "Diana", "Thomas", "Jason", "Carol"]
Local \$Array2 = [3, 4, 2, 6, 7, 1, 9, 10, 5, 8]

If UBound(\$Array1) <> UBound(\$Array2) Then
MsgBox(0, "", "Big mistake")
Else
Local \$Array[UBound(\$Array1)][2]
For \$i = 0 To UBound(\$Array1) - 1
\$Array[\$i][0] = \$Array1[\$i]
\$Array[\$i][1] = \$Array2[\$i]
Next
; optionally free both partial arrays
\$Array1 = 0
\$Array2 = 0
_ArraySort(\$Array)
_ArrayDisplay(\$Array, "Sorted in Alphabetical Order")
EndIf```
1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share on other sites

And with that, all my problems are solved.

Thanks jchd

Create an account

Register a new account

Followers 0

• Similar Content

• By amphoric
Hi,
I am struggling converting my 1D array to a 2D array and then showing that in my List View. Below is my current code, where I would like to take the array from the input box, and have that displayed as a list within the list view this is a list of computer names, and then update the 2nd column of each line once I have ran a ping and a registry change on each computer name, with either 'Completed' and 'Failed'
I have checked out the AutoIt Arrays page here: https://www.autoitscript.com/wiki/Arrays , but unfortunately I cannot get my head around it. Any help would be greatly appreciated.
I hope this makes sense.
Thanks

• By MrCheese
Hey Guys,

So, the functions work, where primarygui() accurately determines the evaluation of the status of the checkboxes - the msgbox picks this up.

However, later on, when we re-enter a 'for \$i = 0 to ubound(\$checkbox)' loop, then in the 'batchinitial' function it doesn't picked up that the status&\$i = 1, so it jumps out, then within the While 1 loop, it exits the loop in the first row, again because the status& \$i= 0
The "assign" line within the primarygui funtion, is this only a local assignment? if so, how can I make it cross function?

Func excelsheetlist() \$i = 0 Global \$aWorkSheets = _Excel_SheetList(\$oWorkbook1) If @error Then Exit MsgBox(\$MB_SYSTEMMODAL, "Excel UDF: _Excel_SheetList Example 1", "Error listing Worksheets." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;_ArrayDisplay(\$aWorkSheets, "Excel UDF: _Excel_SheetList Example 1") ;_ArrayDisplay(\$aWorkSheets, "Array") Global \$iRows = UBound(\$aWorkSheets, \$UBOUND_ROWS) ; Total number of rows. In this example it will be 10. Global \$iCols = UBound(\$aWorkSheets, \$UBOUND_COLUMNS) ; Total number of columns. In this example it will be 20. Global \$iDimension = UBound(\$aWorkSheets, \$UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. MsgBox(\$MB_SYSTEMMODAL, "", "The array is a " & \$iDimension & " dimensional array with " & _ \$iRows & " row(s) & " & \$iCols & " column(s).") Dim \$checkbox[\$iRows] EndFunc ;==>excelsheetlist Func primarygui() ; Create a GUI with various controls. Local \$hGUI = GUICreate("Script Controller", 300, (\$iRows * 24)) ; Create a checkbox control. ;Local \$idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25) Local \$Button2 = GUICtrlCreateButton("Close", 210, 200, 85, 25) Local \$Button3 = GUICtrlCreateButton("Run", 210, 170, 85, 25) Local \$Button1 = GUICtrlCreateButton("Discharge", 210, 140, 85, 25) For \$i = 0 To UBound(\$checkbox) - 1 \$checkbox[\$i] = GUICtrlCreateCheckbox(\$aWorkSheets[\$i][0], 8, 0 + (\$i * 24)) ;, 81, 17) Next ; Display the GUI. GUISetState(@SW_SHOW, \$hGUI) ; Loop until the user exits. While 1 \$nMsg = GUIGetMsg() Select Case \$nMsg = \$GUI_EVENT_CLOSE Exit Case \$nMsg = \$Button2 ;Close Exit Case \$nMsg = \$Button1 MsgBox(0, "Discharge Button not configured", "Now Exiting") Exit Case \$nMsg = \$Button3 ;Run \$fSelection = False For \$i = 0 To UBound(\$checkbox) - 1 If BitAND(GUICtrlRead(\$checkbox[\$i]), \$GUI_CHECKED) Then \$fSelection = True ExitLoop EndIf Next If \$fSelection Then For \$i = 0 To UBound(\$checkbox) - 1 Assign("status" & \$i, GUICtrlRead(\$checkbox[\$i])) Next \$batchcount = 0 For \$i = 0 To UBound(\$checkbox) - 1 If Eval("status" & \$i) = 1 Then \$batchcount = \$batchcount + 1 ;Call ("o" & \$i & "copy") ; if you want to call the functions directly, remove ; before the call and comment or delete the following DirCopy statement MsgBox(0, "Checking", "Checking that: " & \$checkbox[\$i] & " no, with title: " & \$aWorkSheets[\$i][0] & " was selected, Batch count: " & \$batchcount) ; if you need only the DirCopy EndIf Next ExitLoop Else MsgBox(48, 'No Items Selected', 'You have not selected any Patients to Load, Please select from the list') EndIf EndSelect WEnd ; Delete the previous GUI and all controls. GUIDelete(\$hGUI) EndFunc ;==>primarygui Func _IsChecked(\$idControlID) Return BitAND(GUICtrlRead(\$idControlID), \$GUI_CHECKED) = \$GUI_CHECKED EndFunc ;==>_IsChecked Func batchinitial() If Eval("status" & \$i) = 1 Then debugbox() \$oWorkbook1.Sheets(1).Activate \$bigloop = \$bigloop + 1 \$sheet = \$aWorkSheets[\$i][0] \$oWorkbook1.Sheets(\$sheet).Activate debugbox() EndIf EndFunc ;==>batchinitial OpenExcel() excelsheetlist() primarygui() For \$i = 0 To UBound(\$checkbox) - 1 batchinitial() While 1 If Eval("status" & \$i) = 0 Then ExitLoop ;all the rest of my script loops etc WEnd Next
• By FMS
Hello,
I'm building whit mine limited coding know-how a AI in Autoit....
Just because..... why not
It's not doing what i tought it would do.
I hope somebody could help me whit this script?
so far :
-building an learning grid --> AI needs to guess the label in the grin whit only the X and Y value.
-Building an array filled whit random values as weights.
-quess the label
-learn - if quess not the same as the label go change the weights

At this point (the changing of the weights) I've some strange result and hope somebody could point me in the right direction
I think that the problem is in the formula for changing the weights.

PS. I'm also open for good coding practice   I'm learning coding as i go
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <MsgBoxConstants.au3> #include <Array.au3> build_grid_list() build_neurons() _ArrayDisplay(\$gridlist,"\$gridlist 0","",32) _ArrayDisplay(\$neurons,"\$neurons 0","",32) For \$i = 1 To UBound(\$gridlist,1) - 1 If \$gridlist[\$i][3] = 1 Then go_learn(\$neurons[1][0],\$gridlist[\$i][0]) EndIf Next _ArrayDisplay(\$gridlist,"\$gridlist 1","",32) _ArrayDisplay(\$neurons,"q\$neurons 1","",32) Func go_learn(\$neuron_ID,\$gridlist_ID) Local \$test = guess(\$gridlist_ID , \$neuron_ID ) For \$i = 1 To 5000 If \$test <> \$gridlist[\$gridlist_ID][4] Then change_wieght(\$neuron_ID , \$gridlist_ID ) \$test = guess(\$gridlist_ID , \$neuron_ID ) Else ExitLoop EndIf Next EndFunc Func guess(\$gridlist_ID , \$neuron_ID = 1 ,\$raw = 0);, \$loop = 0 ) Local \$temp = 0 ;~ For \$i = 1 To ubound(\$neurons,1) -1 ; input1 * W1 + input2 * W2 + Bias(1) * W3 \$temp = \$gridlist[\$gridlist_ID][1] * \$neurons[\$neuron_ID][1] + \$gridlist[\$gridlist_ID][2] * \$neurons[\$neuron_ID][2] + 1 * \$neurons[\$neuron_ID][3] ;activate (sign) If \$temp >= 0 Then \$neurons[\$neuron_ID][6] = 1 Else \$neurons[\$neuron_ID][6] = -1 EndIf \$gridlist[\$gridlist_ID][5] = \$neurons[\$neuron_ID][6] \$gridlist[\$gridlist_ID][6] = \$gridlist[\$gridlist_ID][6] + 1 \$neurons[\$neuron_ID][9] = \$neurons[\$neuron_ID][9] + 1 If \$raw = 0 Then Return \$neurons[\$neuron_ID][6] Else Return \$temp EndIf EndFunc Func build_neurons(\$needed_neurons = 10 ) Global \$neurons[\$needed_neurons + 1][10] \$neurons[0][0] = "id" \$neurons[0][1] = "wieght1" \$neurons[0][2] = "wieght2" \$neurons[0][3] = "wieght3" \$neurons[0][4] = "input1" \$neurons[0][5] = "input2" \$neurons[0][6] = "output" \$neurons[0][7] = "tweak_counter" \$neurons[0][8] = "not_tweak_counter" \$neurons[0][9] = "quess_counter" Local \$temp = 1 For \$i = 1 To \$needed_neurons; -1 \$neurons[\$i][0] = \$i ;id Do ;zero_check output wieght1 \$temp = Random(-1 , 1) Until \$temp <> 0 \$neurons[\$i][1] = \$temp ;wieght1 Do ;zero_check output wieght2 \$temp = Random(-1 , 1) Until \$temp <> 0 \$neurons[\$i][2] = \$temp ;wieght2 Do ;zero_check output wieght3 \$temp = Random(-1 , 1) Until \$temp <> 0 \$neurons[\$i][3] = \$temp ;wieght3 \$neurons[\$i][4] = 0 ;input1 \$neurons[\$i][5] = 0 ;input2 Do ;zero_check output \$temp = Random(-1 , 1 , 1) Until \$temp <> 0 \$neurons[\$i][6] = \$temp ;output +1 / -1 ;~ \$neurons[\$i][6] = 0 ;output \$neurons[\$i][7] = 0 ;tweak_counter \$neurons[\$i][8] = 0 ;not_tweak_counter \$neurons[\$i][9] = 0 ;quess_counter Next EndFunc Func build_grid_list(\$grid_x = 10 ,\$grid_y = 10 ) Global \$gridlist[(\$grid_x * \$grid_y) + 1 ][7] Local \$counter = 1 \$gridlist[0][0] = "ID" \$gridlist[0][1] = "X" \$gridlist[0][2] = "Y" \$gridlist[0][3] = "Active" \$gridlist[0][4] = "Label" \$gridlist[0][5] = "quessed" \$gridlist[0][6] = "quessed_counter" For \$x = 0 to \$grid_x - 1 For \$y = 0 to \$grid_y - 1 \$gridlist[\$counter][0] = \$counter \$gridlist[\$counter][1] = \$x \$gridlist[\$counter][2] = \$y If Random(-1 , 1) >= 0 Then \$gridlist[\$counter][3] = 0 Else \$gridlist[\$counter][3] = 1 EndIf If \$x > \$y Then \$gridlist[\$counter][4] = 1 Else \$gridlist[\$counter][4] = -1 EndIf \$gridlist[\$counter][5] = -99 \$gridlist[\$counter][6] = 0 \$counter = \$counter + 1 Next Next EndFunc Func change_wieght(\$neuron_id , \$grid_id );, \$W1 , \$W2 ) ;W1 = W1 + ^W1 (some change in W1) ;^W = err(known) * input (\$neurons[\$id][3] = "output") * learningrate ;\$neurons[\$id][1] = \$neurons[\$id][1] + "wieght1" Local \$iReturn = False ; Desired | Quess | Error ; -1 -1 0 ; -1 +1 -2 ; +1 -1 +2 ; +1 +1 0 Local \$error = \$gridlist[\$grid_id][4] - \$neurons[\$neuron_id][6] If \$error <> 0 Then Local \$learningrate = 0.1 Local \$str_len1 = StringLen(\$neurons[\$neuron_id][1]) Local \$str_len2 = StringLen(\$neurons[\$neuron_id][2]) Local \$str_len3 = StringLen(\$neurons[\$neuron_id][3]) Local \$dif_weights1 = \$error * \$neurons[\$neuron_id][1] * \$learningrate Local \$dif_weights2 = \$error * \$neurons[\$neuron_id][2] * \$learningrate Local \$dif_weights3 = \$error * \$neurons[\$neuron_id][3] * \$learningrate Local \$verschil1 = StringLeft(\$dif_weights1,\$str_len1) Local \$verschil2 = StringLeft(\$dif_weights2,\$str_len2) Local \$verschil3 = StringLeft(\$dif_weights3,\$str_len3) Local \$new_wieght1 = \$neurons[\$neuron_id][1] + \$verschil1 Local \$new_wieght2 = \$neurons[\$neuron_id][2] + \$verschil2 Local \$new_wieght3 = \$neurons[\$neuron_id][3] + \$verschil3 \$neurons[\$neuron_id][1] = StringLeft(\$new_wieght1,\$str_len1) \$neurons[\$neuron_id][2] = StringLeft(\$new_wieght2,\$str_len2) \$neurons[\$neuron_id][3] = StringLeft(\$new_wieght3,\$str_len3) \$neurons[\$neuron_id][7] = \$neurons[\$neuron_id][7] + 1 ;"counter" \$iReturn = False Else \$neurons[\$neuron_id][8] = \$neurons[\$neuron_id][8] + 1 ;"not counter" \$iReturn = True EndIf Return \$iReturn EndFunc thanks in advanced.

• By Dimmae
Hello,
at first: i'm new here, so please forgive me my mistakes, and show them to me, just that i can learn to do better in the future.
Now to my Problem: i have an excel sheet, where i just need some columns for further actions, but i have no idea how to add single columns to a new array.
I found the following code(the one i just added as a file) from 'water' in this forum, but i wont get how i could add multiple columns into a new array.
The biggest problem in my situation is that i dont know the count of the rows i need for the array, i just got a fix number of rows, which is 4.

Hope you can help me, and sry again for this 'unlucky illustration'.

btw: how can i add code shown as code here, instead of posting it as a attached file?.

autoit-select-column.au3
defects.xlsx
• By czardas
Haven't had much time to code recently. However the following thread inspired me.
The debate about linear, parallel and binary search methods was rather interesting and, in an attempt to be diplomatic, I decided to combine @jchd's suggestion with @LarsJ's binary search example. I decided that the binary search algorithm required modification to make it more linear. As usual, 'if you invent something, it probably already exists and if it already exists, it exists for a reason'. My first attempt was not all that good. The code worked but was really a mess. I blame peer pressure (to post an example of a parallel search method).  I will delete that old code in due course.
With a little memory jogging and a glance at the help file, the solution turned out to be quite easy: I just needed a better understanding of Euler. Further modification will be needed to work with more complicated unicode strings. The output could be returned as an array or a delimitered string. I'm not so interested in those details. I'm just going to post the algorithm for now and anyone, who wants to, can modify it to suit their needs. Both arrays must contain at least 1 element.
Local \$aFoo = [0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,19,20,23,24,26,30,35,39,40,41] Local \$aBar = [0,1,5,6,7,8,9,10,11,12,13,14,17,18,19,21,24,25,26,27,34,35,38,40] ParallelExponetialSearch(\$aFoo, \$aBar) ; Compares two lists - returning positive matches. Each input array must be unique (individually) and in alphabetical order. Func ParallelExponetialSearch(\$aFoo, \$aBar) Local \$sFind, _ \$iMin_F = -1, \$iMax_F = UBound(\$aFoo) -1, \$Lo_F = \$iMin_F, \$Hi_F, _ \$iMin_B = -1, \$iMax_B = UBound(\$aBar) -1, \$Lo_B = \$iMin_B, \$Hi_B While \$iMin_F < \$iMax_F And \$iMin_B < \$iMax_B ; Toggle Arrays - Which array has most untested elements? This is the one we want to search next, ; so we can bypass more comparisons because (in theory) mismatches have a greater chance of being skipped. If \$iMax_F - \$iMin_F >= \$iMax_B - \$iMin_B Then ; \$aFoo has more (or an equal number of) untested elements \$Hi_F = \$iMax_F \$iMin_B += 1 \$sFind = \$aBar[\$iMin_B] While \$Lo_F < \$Hi_F ; search \$aFoo For \$i = 0 To Floor(Log(\$Hi_F - \$Lo_F) / Log(2)) \$Lo_F = \$iMin_F + 2^\$i If \$aFoo[\$Lo_F] = \$sFind Then \$iMin_F = \$Lo_F ; each match should be added to the output [perhaps an array] ConsoleWrite(\$sFind & " found at \$aFoo[" & \$Lo_F & "] = \$aBar[" & \$iMin_B & "]" & @LF) ExitLoop 2 ElseIf \$aFoo[\$Lo_F] > \$sFind Then \$Hi_F = \$Lo_F -1 \$iMin_F += Floor(2^(\$i -1)) \$Lo_F = \$iMin_F ContinueLoop 2 EndIf Next \$iMin_F = \$Lo_F ; minimum increment is one WEnd Else ; \$aBar has more untested elements \$Hi_B = \$iMax_B \$iMin_F += 1 \$sFind = \$aFoo[\$iMin_F] While \$Lo_B < \$Hi_B ; search \$aBar For \$i = 0 To Floor(Log(\$Hi_B - \$Lo_B) / Log(2)) \$Lo_B = \$iMin_B + 2^\$i If \$aBar[\$Lo_B] = \$sFind Then \$iMin_B = \$Lo_B ; each match should be added to the output [perhaps an array] ConsoleWrite(\$sFind & " found at \$aFoo[" & \$iMin_F & "] = \$aBar[" & \$Lo_B & "]" & @LF) ExitLoop 2 ElseIf \$aBar[\$Lo_B] > \$sFind Then \$Hi_B = \$Lo_B -1 \$iMin_B += Floor(2^(\$i -1)) \$Lo_B = \$iMin_B ContinueLoop 2 EndIf Next \$iMin_B = \$Lo_B ; minimum increment is one WEnd EndIf WEnd EndFunc ;==> ParallelExponetialSearch I hope this will be useful to someone. I believe it deserved a thread of its own!