Re-ordering a simple array

Recommended Posts

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

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")```

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

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

• Similar Content

• By Blitzkid
Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
to smth. like

Here is my Code
#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> \$filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local \$arr[3] = ["music", "picture", "video"] For \$i = 0 To UBound(\$arr) - 1 Local \$arrayfiles = _FileListToArray(\$filedir & \$arr[\$i], \$arr[\$i] & "*", 1) If @error = 1 Then ConsoleWrite(\$arr[\$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite(\$arr[\$i] & "Error 2") ;Exit EndIf \$arrayfilter = _ArrayMax(\$arrayfiles, 0, 1) Global \$stringfiles = StringReplace(\$arrayfilter, \$arr[\$i], "") ConsoleWrite(\$arrayfilter & @CRLF) Next EndFunc ;==>_checkfile
• By jmp
Script running good but error in line 7.
When i run this script :
#include <IE.au3> #include <Array.au3> \$oIE = _IEAttach ("Shop") \$oTable = _IETableGetCollection (\$oIE, 1) \$aTableData = _IETableWriteToArray (\$oTable) For \$inumber = 1 To UBound(\$aTableData) -1 \$table = \$aTableData[4][\$inumber] MsgBox(0, "", \$table) Next I got Error: array variable has incorrect number of subscripts or subscript dimension range exceeded

• Good morning guys, i hope that you're all well.
guys, i have a problem and i hope that you can help me
i've created an 3d array
the array Contain a Categories info
as folow
\$array[n][0][0] = Categorie name
\$array[n][0][1] = Categorie file path
\$array[n][0][2] = Categorie contents number
in my tool i want to add an option to delete a Category
as you know the Categorie mean that must delete a region from the array
when i tried to use _arrayDelete
with the 2d array it work well
but here i couldn't find any way to do that, can any one help me please?

i tried to create a function that read a folder files to 3d array
e.g
\$array[n][0][0] = ctName
\$array[n][0][1] = ctFilePath
\$array[n][0][2] = crtsections number
\$array[n][m][0] = KeyName
\$array[n][m][1] = KeyVal
\$array[n][m][2] = keySectionName

that the array
when i put one file into the folder all things work fine
but when i put more than one file
the last file worked fine but the others only the first key is showing
please can you help me to correct this problem
here is the example with the folder

array3d.zip

• good morning everybody.
today i liked to share an small example with you
which it an function to read the registry values as an array
the result array is 2d array witch
\$a_array[n][0] = value name
\$a_array[n][1] = value Data
\$a_array[0][0] = values count
here's the function

#include <Array.au3> #include <WinAPIReg.au3> #include <APIRegConstants.au3> Local \$a_array = _RegReadToArray("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run") If @error Then     MsgBox(16, "error", @error)     Exit EndIf _ArrayDisplay(\$a_array) Func _RegReadToArray(\$s_RegKey)     Local \$a_KeySplitInfo = StringSplit(\$s_RegKey, "\\", 2)     If UBound(\$a_KeySplitInfo) <= 1 Then         \$a_KeySplitInfo = StringSplit(\$s_RegKey, "\", 2)         If UBound(\$a_KeySplitInfo) <= 1 Then Return (1, 1, 0)     EndIf     Local \$H_KeyInfo = "", \$s_RegKeyInfo = ""     Switch \$a_KeySplitInfo[0]         Case "hklm", "HKEY_LOCAL_MACHINE", "hklm64", "HKEY_LOCAL_MACHINE64"             \$H_KeyInfo = \$HKEY_LOCAL_MACHINE         Case "hkCu", "HKEY_CURRENT_USER", "hkCU64", "HKEY_CURRENT_USER64"             \$H_KeyInfo = \$HKEY_CURRENT_USER         Case "hkCr", "HKEY_CLASSES_ROOT", "HKCR64", "HKEY_CLASSES_ROOT64"             \$H_KeyInfo = \$HKEY_CLASSES_ROOT         Case "HKU", "HKEY_USERS", "HKU64", "HKEY_USERS64"             \$H_KeyInfo = \$HKEY_USERS         Case Else             Return SetError(2, 2, 0)     EndSwitch     _ArrayDelete(\$a_KeySplitInfo, 0)     \$s_RegKeyInfo = _ArrayToString(\$a_KeySplitInfo, "\")     Local \$H_KeyInfoOpen = _WinAPI_RegOpenKey(\$H_KeyInfo, \$s_RegKeyInfo, \$KEY_READ)     Local \$A_KeyInfo = _WinAPI_RegQueryInfoKey(\$H_KeyInfoOpen)     If @error Then Return SetError(1, 1, 0)     _WinAPI_RegCloseKey(\$H_KeyInfoOpen)     Local \$A_RegVal[\$A_KeyInfo[2] + 1][2]     Local \$iV = 1, \$s_RegRead = ""     While 1         \$s_RegVal = RegEnumVal(\$s_RegKey, \$iV)         If @error <> 0 Then ExitLoop         \$s_RegRead = RegRead(\$s_RegKey, \$s_RegVal)         If Not (@error) Then             \$A_RegVal[\$iV][0] = \$s_RegVal             \$A_RegVal[\$iV][1] = \$s_RegRead         EndIf         \$iV += 1     WEnd     \$A_RegVal[0][0] = UBound(\$A_RegVal) - 1     If \$A_RegVal[0][0] >= 1 Then         Return \$A_RegVal     Else         Return SetError(3, 3, 0)     EndIf EndFunc   ;==>_RegReadToArray
i hope you benefit from it
with my greetings
×
×
• Create New...