Jump to content

Re-ordering a simple array


Go to solution Solved by jchd,

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

Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
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

Link to comment
Share on other sites

  • Solution

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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

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

×
×
  • Create New...