knucklesCZ Posted March 1, 2015 Posted March 1, 2015 Hi coders, I'm having trouble with arrays. I decided to stay at 1D array because I think it's simpler but I got a problem as seen here: the array contains elements like this: "thingX-XX Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] The result should be this: Local $array = ["thing4-05", "thing3-07",..., "thing5-10"] Any ideas how to arrange that like this? Thank you in advance.
Moderators Melba23 Posted March 1, 2015 Moderators Posted March 1, 2015 knucklesCZ,This is one way of doing it: #include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] ; Add a new column _ArrayColInsert($array, 1) For $i = 0 To UBound($array) - 1 ; Extract numeric part and add to new column $array[$i][1] = StringRegExpReplace($array[$i][0], ".*-(.*)", "$1") Next _ArrayDisplay($array, "Col added", Default, 8) ; Sort on the number column _ArraySort($array, Default, Default, Default, 1) _ArrayDisplay($array, "Sorted", Default, 8) ; Delete the column _ArrayColDelete($array, 1) _ArrayDisplay($array, "Col deleted", Default, 8)M23 knucklesCZ 1 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
knucklesCZ Posted March 1, 2015 Author Posted March 1, 2015 (edited) Thanks, I really appreciate the help Edited March 1, 2015 by knucklesCZ
knucklesCZ Posted March 1, 2015 Author Posted March 1, 2015 (edited) Well actually after sorting, the array remains a 2D array (with 2 cols) I think that can be also seen in the _ArrayDisplay table where it says the array is $array[5][1] in the final step where the second col is deleted. Thus the array should be 1D (1 col array) again. but I can't use it as 1D --> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: MsgBox($MB_SYSTEMMODAL, "", $array[0]) MsgBox($MB_SYSTEMMODAL, "", ^ ERROR Instead, 2D things are working MsgBox($MB_SYSTEMMODAL, "", $array[0][0]) <--- that goes fine That's strange.+ only this works (@down) MsgBox($MB_SYSTEMMODAL, "", $array[0][0]) MsgBox($MB_SYSTEMMODAL, "", $array[1][0]) MsgBox($MB_SYSTEMMODAL, "", $array[2][0]) this doesn't though (@down) MsgBox($MB_SYSTEMMODAL, "", $array[0][0]) MsgBox($MB_SYSTEMMODAL, "", $array[1][1]) MsgBox($MB_SYSTEMMODAL, "", $array[2][2]) Edited March 1, 2015 by knucklesCZ
Trong Posted March 1, 2015 Posted March 1, 2015 Convert 2D arrays to 1D array #include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] _ArrayDisplay($array, "No Sort", Default, 8) _ArrayColInsert($array, 1) For $i = 0 To UBound($array) - 1 $array[$i][1] = StringRegExpReplace($array[$i][0], ".*-(.*)", "$1") Next _ArraySort($array, Default, Default, Default, 1) _ArrayColDelete($array, 1) Local $a,$NewArray[UBound($array)] For $i = 0 To UBound($array) - 1 $NewArray[$i] =$array[$i][0] Next $array=$NewArray _ArrayDisplay($array, "Sorted", Default, 8) MsgBox(0, "", $array[0]) Regards,
mikell Posted March 1, 2015 Posted March 1, 2015 #include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] _MixArray($array) _ArraySort($array) _MixArray($array) _ArrayDisplay($array) MsgBox(0, "", $array[0]) Func _MixArray(ByRef $array) For $i = 0 To UBound($array) - 1 $array[$i] = StringRegExpReplace($array[$i], "(.*)(-)(.*)", "$3$2$1") Next EndFunc But you are right, this array trouble is strange indeed knucklesCZ 1
Moderators Melba23 Posted March 1, 2015 Moderators Posted March 1, 2015 knucklesCZ,My fault. As explained in the Help file, you need to use the $bConvert parameter in _ArrayColDelete to force the array back to 1D when there is only a single column:; Delete the column _ArrayColDelete($array, 1, True)And to think that I wrote the function too! :blush:M23 knucklesCZ 1 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
iamtheky Posted March 1, 2015 Posted March 1, 2015 (edited) i dont have time to test if this lucky for this particular arrangement, but #include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] for $i = 1 to ubound($array) - 1 If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) > number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then _arrayswap($array , $i , $i - 1) $i = 0 EndIf next _ArrayDisplay($array) Edited March 1, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Trong Posted March 1, 2015 Posted March 1, 2015 (edited) i dont have time to test if this lucky for this particular arrangement, but#include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] for $i = 1 to ubound($array) - 1 If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) > number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then _arrayswap($array , $i , $i - 1) $i = 0 EndIf next _ArrayDisplay($array)descending Edited March 1, 2015 by Trong Regards,
iamtheky Posted March 1, 2015 Posted March 1, 2015 (edited) #include <Array.au3> Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"] for $i = 1 to ubound($array) - 1 If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) < number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then _arrayswap($array , $i , $i - 1) $i = 0 EndIf next _ArrayDisplay($array) just flip the sign, my bad. and it seems to hold up in prelim testing Edited March 1, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
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