NoTolerance Posted October 4, 2013 Share Posted October 4, 2013 I need to sort an array by file size so I can grab the largest file in the directory first. So I've taken a standard array of file names built by using _FileListToArray and turned it into a 2D array: Local $eSize[$recheck[0]][2] I then used a For loop and FileGetSize to get my file sizes: For $E = 1 To $recheck[0] $tmpSize = (FileGetSize($full_path & "\" & $recheck[$E])) / 1048576 $eSize[$E - 1][0] = $tmpSize $eSize[$E - 1][1] = $recheck[$E] Next When I use _ArraySort($eSize, 1), I get the results I'm expecting: the files sorted by size in descending order. Now I need to make use of the file names in my next For loop, so I want to drop the file size column. I found a snippet of code >here that seems to accomplish this task: Func _ArrayDeleteCol(ByRef $avWork, $iCol) If Not IsArray($avWork) Then Return SetError(1, 0, 0); Not an array If UBound($avWork, 0) <> 2 Then Return SetError(1, 1, 0); Not a 2D array If ($iCol < 0) Or ($iCol > (UBound($avWork, 2) - 1)) Then Return SetError(1, 2, 0); $iCol out of range If $iCol < UBound($avWork, 2) - 1 Then For $c = $iCol To UBound($avWork, 2) - 2 For $r = 0 To UBound($avWork) - 1 $avWork[$r][$c] = $avWork[$r][$c + 1] Next Next EndIf ReDim $avWork[UBound($avWork)][UBound($avWork, 2) - 1] Return 1 EndFunc I implement the function: _ArrayDeleteCol($eSize, UBound($eSize, 2) - 2) (I've also tried _ArrayDeleteCol($avArray, 0) and have the same results.) When I check the array (_ArrayDisplay) it all looks good: I see my file names, still sorted, but the file size column has been removed. However, something seems to "break" within the array where now, even though it looks like a standard 1D array, it's being recognized as a 2D array. If I were to do something like ConsoleWrite($eSize[0]), for example, I get: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: ConsoleWrite($eSize[0]) ConsoleWrite(^ ERROR Any idea what's going on here and how I can fix or work around it? To summarize my goal: Build an array of file names in a directory Sort that array by file size, largest first Cycle through the array in a For loop to do some logic testing Perhaps there's an easier, more efficient way to do what I'm attempting - I'm just not seeing it. Nor can I see where I'm going wrong with this code. Link to comment Share on other sites More sharing options...
Developers Jos Posted October 4, 2013 Developers Share Posted October 4, 2013 not sure why you need to drop the size column in the array to be able to use the filename? Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted October 4, 2013 Moderators Solution Share Posted October 4, 2013 NoTolerance,Although they look the same there is a difference between a 1D array and a 2D array with 1 column:#include <Array.au3> Global $aArray_1D[3] = [0, 1, 2] _ArrayDisplay($aArray_1D, "1D") Global $aArray_2D[3][1] = [[0], [1], [2]] _ArrayDisplay($aArray_2D, "2D")If you declare an array as 2D then removing columns still keeps the 2 dimensions and so you need to use both to identify the elements. If you were to use a loop to rewrite the original array with the sorted names you would get a 1D array to deal with. Otherwise you will have to use [#][0] to identify the elements in the single-column 2D array. 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 Link to comment Share on other sites More sharing options...
NoTolerance Posted October 4, 2013 Author Share Posted October 4, 2013 (edited) not sure why you need to drop the size column in the array to be able to use the filename? Jos Honestly? Because like most beginners, I find arrays to be moderately confusing and couldn't find a good explanation on how to just grab what I was after in the 2D array. Looks like Melba pointed me in the right direction below, though.... NoTolerance, Although they look the same there is a difference between a 1D array and a 2D array with 1 column: #include <Array.au3> Global $aArray_1D[3] = [0, 1, 2] _ArrayDisplay($aArray_1D, "1D") Global $aArray_2D[3][1] = [[0], [1], [2]] _ArrayDisplay($aArray_2D, "2D") If you declare an array as 2D then removing columns still keeps the 2 dimensions and so you need to use both to identify the elements. If you were to use a loop to rewrite the original array with the sorted names you would get a 1D array to deal with. Otherwise you will have to use [#][0] to identify the elements in the single-column 2D array. M23 Thank you for the concise explanation! So I understand more clearly, when you say, "you will have to use [#][0] to identify the elements in the single-column 2D array", am I correct in assuming that you're using # as a placeholder for the row number? e.g. I would use [1][0] to get the second row value in your sample code? This is where I initially got confused and why I attempted to just "simplify" by dropping the unneeded column... Edited October 4, 2013 by NoTolerance Link to comment Share on other sites More sharing options...
Developers Jos Posted October 4, 2013 Developers Share Posted October 4, 2013 Think of arrays as a Spreadsheet: Array[Rows][Columns][Tabs] In most cases you should be able to work with a 2d Array, just rows and columns. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
NoTolerance Posted October 4, 2013 Author Share Posted October 4, 2013 So I understand more clearly, when you say, "you will have to use [#][0] to identify the elements in the single-column 2D array", am I correct in assuming that you're using # as a placeholder for the row number? e.g. I would use [1][0] to get the second row value in your sample code? This is where I initially got confused and why I attempted to just "simplify" by dropping the unneeded column... I tested and answered my own question, which I should have done in the first place - sorry. "Yes", for the drive-bys! Link to comment Share on other sites More sharing options...
NoTolerance Posted October 4, 2013 Author Share Posted October 4, 2013 Think of arrays as a Spreadsheet: Array[Rows][Columns][Tabs] In most cases you should be able to work with a 2d Array, just rows and columns. Jos Thanks! I'm not doing anything overly complicated, so this need for a 2D array is rare - hence my confusion. While I've had to work with all sorts of 1D arrays, this is my first time having to work with a 2D, and I think I was just over-thinking it. I blame this blasted head cold. lol Link to comment Share on other sites More sharing options...
kylomas Posted October 4, 2013 Share Posted October 4, 2013 (edited) NoTolerence, As Jos suggested, don't bother dropping a column in the 2D array, just reference whatever column you are interested in, e.g. $myfile = $array[10][0] to get the first column of row 10. kylomas edit: Jeez, 4 postes marked "Posted a minute ago". Talk about customer service (or my slow typing). Edited October 4, 2013 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
NoTolerance Posted October 4, 2013 Author Share Posted October 4, 2013 edit: Jeez, 4 postes marked "Posted a minute ago". Talk about customer service (or my slow typing). That's okay - I appreciate the confirmation nonetheless! Link to comment Share on other sites More sharing options...
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