Jump to content

Combining two arrays into a 2d Array


Yuushi
 Share

Recommended Posts

Hey all,

I've been breaking my brain over this fairly simple idea but i cant seem to figure out how to do it. Here's what I want to do:

Lets say I have two arrays: $numberarray[5] and $namearray[5]

numberarray holds the value's: 5, 10,60,70

namearray holds the value's: Tommy, Duka, Bill, Bob

What I want to do is make a 2d array that would combine these results to:

Tommy 5

Duka 10

Bill 60

Bob 70

Been trying to figure out how but I'm lost atm..

Any help is welcome :)

Regards,

Yuushi

Link to comment
Share on other sites

  • Moderators

Yuushi,

Try something along these lines:

; Create your 2D array
Local $2D_Array[UBound($numberarray)][2]

; Then get the variables into it
For $i = 1 To UBound($numberarray)
    $2D_Array[$i][0] = $namearray[$i]
    $2D_Array[$i][1] = $numberarray[$i]
Next

; And then delete $name and $number if you no longer need them
$namearray = 0
$numberarray = 0

I have not tested this code, but the basic idea is sound.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Yuushi,

A quick reply! Look at _ArraySort in the help file!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thnx again but I am having some difficulty here's what I am doing

The array should look something like:

name1                 20
name2                 70
name3                 50
name4                 100

Now my goal is to sort the array based on the numbers in the second row I try this with the following code

; Then get the variables into it
For $i = 0 To $to
    $2D_Array[$i][0] = $buffnamearray[$i]
    $2D_Array[$i][1] = Number($numberarray[$i])
Next

_ArraySort($2D_Array,1,1)
return $2D_Array

My understanding of _ArraySort is:

_ArraySort(nameofarray,1 = descending,index to sort on in this case the second part of the array so value 1)

Maybe I missunderstand something about the value's needed to be defined in ArraySort but I cant seem to figure it out.

Link to comment
Share on other sites

#include <Array.au3>
Opt('MustDeclareVars', 1)

Dim $Arr[4][2] = [['name1',100], ['name2', 20], ['name3', 50], ['name4', 70]]
Dim $i, $j

For $i = 0 To 2
    For $j = 0 To 3-$i-1
        If $Arr[$j][1] > $Arr[$j+1][1] Then
            Local $name = $Arr[$j][0], $age = $Arr[$j][1]
            $Arr[$j][0] = $Arr[$j+1][0]
            $Arr[$j][1] = $Arr[$j+1][1]
            $Arr[$j+1][0] = $name
            $Arr[$j+1][1] = $age
        EndIf
    Next
Next

_ArrayDisplay($Arr, 'Title')

Read about quicksort algorithm if your intention is to sort much bigger array (bigger than 30 subscripts per row)

Link to comment
Share on other sites

#include <Array.au3>
Opt('MustDeclareVars', 1)

Dim $Arr[4][2] = [['name1',100], ['name2', 20], ['name3', 50], ['name4', 70]]
Dim $i, $j

For $i = 0 To 2
    For $j = 0 To 3-$i-1
        If $Arr[$j][1] > $Arr[$j+1][1] Then
            Local $name = $Arr[$j][0], $age = $Arr[$j][1]
            $Arr[$j][0] = $Arr[$j+1][0]
            $Arr[$j][1] = $Arr[$j+1][1]
            $Arr[$j+1][0] = $name
            $Arr[$j+1][1] = $age
        EndIf
    Next
Next

_ArrayDisplay($Arr, 'Title')

The array $numberarray[$i] contains numbers obtained from a textfile at first I thought that the sort didnt work because they where seen as strings but the use of number() would prevent that???

Yuushi

Link to comment
Share on other sites

Sorry, I don't know what is the content of the text file. You'll need to check whether there are no blank spaces in the strings or the number function might return always 0.

If that was the case wouldnt the array show 0 to the actual value's? if I check the value's in the array after I converted them to numbers they all show the correct value's

Anyway i'll continue debugging.

Ty :)

Link to comment
Share on other sites

No problem :).

For $i = 0 To 2
        For $j = 0 To 3-$i-1
            If $2D_Array[$j][1] > $2D_Array[$j+1][1] Then
                Local $name = $2D_Array[$j][0], $age = $2D_Array[$j][1]
                $2D_Array[$j][0] = $2D_Array[$j+1][0]
                $2D_Array[$j][1] = $2D_Array[$j+1][1]
                $2D_Array[$j+1][0] = $name
                $2D_Array[$j+1][1] = $age
            EndIf
        Next
    Next

You'll need to change the max subscript loop in the outer as well as in the inner loops because I see you call it with 2 or 3 but $2D_Array contain $to subscripts =P

Link to comment
Share on other sites

Tell me, how big is your array?

Edit: Meh, never mind.

I've tested it using the AutoITSpeed.au3 posted somewhere in this forum. Judge yourself ;]

#include <Array.au3>
Opt('MustDeclareVars', 1)
Dim $Arr[0x400][2], $i, $j

$j = 0
For $i = 0x3FF To 0 Step -1
    $j += 1
    $Arr[$i][0] = 'Name' & $i
    $Arr[$i][1] = $j
Next

_ArrayDisplay($Arr, 'Unsorted')

QuickSort($Arr, 0, 0x3FF)

_ArrayDisplay($Arr, 'Sorted ascending')

Exit




Func QuickSort(ByRef $Arr, $lo, $hi)
    Local $i = $lo, $j = $hi, $x = $Arr[($lo+$hi)/2][1]
    
    While $i <= $j
        While $Arr[$i][1] < $x
            $i += 1
        WEnd
        
        While $Arr[$j][1] > $x
            $j -= 1
        WEnd
        
        If $i <= $j Then
            Local $name = $Arr[$i][0]
            Local $age = $Arr[$i][1]
            $Arr[$i][0] = $Arr[$j][0]
            $Arr[$i][1] = $Arr[$j][1]
            $Arr[$j][0] = $name
            $Arr[$j][1] = $age
            $i += 1
            $j -= 1
        EndIf
    WEnd
    
    If $lo < $j Then QuickSort($Arr, $lo, $j)
    If $i < $hi Then QuickSort($Arr, $i, $hi)
    
EndFunc
Edited by Authenticity
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...