Yuushi Posted January 31, 2009 Posted January 31, 2009 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
Moderators Melba23 Posted January 31, 2009 Moderators Posted January 31, 2009 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Yuushi Posted January 31, 2009 Author Posted January 31, 2009 That worked perfectly and so easy rofl thank yoU!
Yuushi Posted January 31, 2009 Author Posted January 31, 2009 Hmm one more quick question actually would it be possible to order the array based on the number in the second part of the 2d array?
Moderators Melba23 Posted January 31, 2009 Moderators Posted January 31, 2009 Yuushi, A quick reply! Look at _ArraySort in the help file! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Yuushi Posted January 31, 2009 Author Posted January 31, 2009 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.
Authenticity Posted January 31, 2009 Posted January 31, 2009 #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)
Yuushi Posted January 31, 2009 Author Posted January 31, 2009 #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
Authenticity Posted January 31, 2009 Posted January 31, 2009 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.
Yuushi Posted January 31, 2009 Author Posted January 31, 2009 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'sAnyway i'll continue debugging. Ty
Authenticity Posted January 31, 2009 Posted January 31, 2009 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
Yuushi Posted February 1, 2009 Author Posted February 1, 2009 After modifing the size of the loop it worked fine thank you for the help
Authenticity Posted February 1, 2009 Posted February 1, 2009 (edited) 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 ;]expandcollapse popup#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 February 1, 2009 by Authenticity
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now