# Headbreaking; Sort variable array

## Recommended Posts

Hello,

I've spent some hours finding out how to do this, but maybe i'm trying way to long to get the solution. Maybe you guys can help me out.

The situation;

I have 1 array, which is MAX 20 entries long.

Lets see the code;

```#include <array.au3>
local \$array[11]
local \$array2[11]
local \$last[11]
\$array[1] = "abc|123|fff"
\$array[2] = "def|456|ddd"
\$array[3] = "ghi|789|aaa"
\$array[5] = "abc|123|wegwwa"
\$array[6] = "ghi|789|regrerg"
\$array[7] = "ghi|789|ccwerwe"
\$array[8] = "ghi|789|uu984u9"
\$array[9] = "ghi|789|aegoihaweoigw"
\$array[10] = "ghi|789|bla"
for \$x = 1 to 10
\$array2 = \$array[\$x]
............... how?!
Next

_ArrayDisplay(\$last)```

I'm trying to sort this array by using a loop inside a loop but i can't get it to work.

The purpose is that everything that begins with abc, will be sorted in the "last[11]" array.

so:

last[1] will be abc|123|fff

last[2] will be abc|123|adfagew

last[3] will be abc|123|wegwwa

that also counts for def and ghi... Also its good to know that there can be 4096 different variaties for abc, def and ghi... so only selecting them with a case "abc" case "def" won't work.

I made this simple example becoz if i write down all of my code here, you'll be figuring out for ages to find out what i'm trying to do

Thanks in advance for help.

##### Share on other sites

Hi.

You might want to use a 2D array:

```Local \$last[11][3] = [[10], _
["abc", "123", "fff"], _
["def", "456", "ddd"], _
["ghi", "789", "aaa"], _
["abc", "123", "adfagew"], _
["abc", "123", "wegwwa"], _
["ghi", "789", "regrerg"], _
["ghi", "789", "ccwerwe"], _
["ghi", "789", "uu984u9"], _
["ghi", "789", "aegoihaweoigw"], _
["ghi", "789", "bla"]]```

Then use _arraysort()

regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

##### Share on other sites

Hi.

You might want to use a 2D array:

```Local \$last[11][3] = [[10], _
["abc", "123", "fff"], _
["def", "456", "ddd"], _
["ghi", "789", "aaa"], _
["abc", "123", "adfagew"], _
["abc", "123", "wegwwa"], _
["ghi", "789", "regrerg"], _
["ghi", "789", "ccwerwe"], _
["ghi", "789", "uu984u9"], _
["ghi", "789", "aegoihaweoigw"], _
["ghi", "789", "bla"]]```

Then use _arraysort()

regards, Rudi.

K, but the number of entries are also variable. Lets say i'd like to write every abc, def and ghi to a different file but i dont know if [10] is correct. ? Also, the string could be longer than 3 blocks.

##### Share on other sites

or, if you still want to keep your original array string structure, you can use something like this:

```#include <array.au3>

local \$last[1] = [0]
local \$array[11] = [10, _
"abc|123|fff", _
"def|456|ddd", _
"ghi|789|aaa", _
"abc|123|wegwwa", _
"ghi|789|regrerg", _
"ghi|789|ccwerwe", _
"ghi|789|uu984u9", _
"ghi|789|aegoihaweoigw", _
"ghi|789|bla"]

\$last = _ArrayElementsThatStartWith(\$array, "abc")
If NOT @error Then _ArrayDisplay(\$last, "abc - list")

\$last = _ArrayElementsThatStartWith(\$array, "def")
If NOT @error Then _ArrayDisplay(\$last, "def - list")

\$last = _ArrayElementsThatStartWith(\$array, "ghi")
If NOT @error Then _ArrayDisplay(\$last, "ghi - list")

Func _ArrayElementsThatStartWith(\$__array, \$__text)
If NOT IsArray(\$__array) then Return SetError(1, 0, 0)
Local \$ret_val[1] = [0]
for \$x = 1 to UBound(\$__array)-1
If StringLeft(\$__array[\$x], StringLen(\$__text)) = \$__text Then
ReDim \$ret_val[UBound(\$ret_val)+1]
\$ret_val[0] = UBound(\$ret_val)-1
\$ret_val[UBound(\$ret_val)-1] = \$__array[\$x]
EndIf
Next
Return \$ret_val
EndFunc```

##### Share on other sites

Hi.

How do you read the raw data ("abc|123|fff", ...) that have to be processed? From a file?

Pls post an example for your "raw data" and possible variations ("abc|123|793|abx|fff" or "" or "fff" or "aaa|fff" or whatsoever might show up)

And what did you put into array[11] and array2[11], for what purpose?

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

##### Share on other sites

hi notsure

is not very clear to me exactly what you want to accomplish,

perhaps you only need these 2 instructions ?

```#include <array.au3>
Local \$array[11]
Local \$array2[11]
Local \$last[11]
\$array[1] = "abc|123|fff"
\$array[2] = "def|456|ddd"
\$array[3] = "ghi|789|aaa"
\$array[5] = "abc|123|wegwwa"
\$array[6] = "ghi|789|regrerg"
\$array[7] = "ghi|789|ccwerwe"
\$array[8] = "ghi|789|uu984u9"
\$array[9] = "ghi|789|aegoihaweoigw"
\$array[10] = "ghi|789|bla"

\$array2 = \$array
_ArraySort(\$array2)
\$last = \$array2
_ArrayDisplay(\$last)```

bye

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

I am sorry for my above trivial answer,

better reading the request, now it is clearer to me the ultimate goal.
this script is generalized and can fit for different sizes and different elements lengths

```#include <array.au3>
Local \$Elements = 11
Local \$array[\$Elements]
Local \$array2[\$Elements][1]
Local \$last[1]
\$array[1] = "abc|123|fff"
\$array[2] = "def|456|ddd|extra" ; <- an extra element
\$array[3] = "ghi|789|aaa"
\$array[5] = "abc|123|wegwwa"
\$array[6] = "ghi|789|regrerg"
\$array[7] = "ghi|789|ccwerwe"
\$array[8] = "ghi|789|uu984u9"
\$array[9] = "ghi|789|aegoihaweoigw"
\$array[10] = "ghi|789|bla"
Local \$MaxColumns = 0
For \$x = 1 To UBound(\$array) -1
; ............... how?!
\$TmpArray = StringSplit(\$array[\$x], "|", 2)
\$TmpColumns = UBound(\$TmpArray)
If \$MaxColumns < \$TmpColumns Then
\$MaxColumns = \$TmpColumns
ReDim \$array2[\$Elements][\$MaxColumns]
EndIf
For \$y = 0 To \$TmpColumns - 1
\$array2[\$x][\$y] = \$TmpArray[\$y]
Next
Next
\$UniqueElements = _ArrayUnique(\$array2, 1, 1)
_ArrayDelete(\$UniqueElements, 0)
For \$x = 0 To UBound(\$UniqueElements) - 1
\$TmpGroup = _ArrayFindAll(\$array2, \$UniqueElements[\$x])
Dim \$last[UBound(\$TmpGroup)]
For \$y = 0 To UBound(\$TmpGroup) - 1
\$last[\$y] = \$array[\$TmpGroup[\$y]]
Next
_ArrayDisplay(\$last)
Next```

I hope can fit for you

bye

Edited by Pincopanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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