Jump to content
Sign in to follow this  
NoTolerance

Issue Dropping Column from Array

Recommended Posts

NoTolerance

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:

  1. Build an array of file names in a directory
  2. Sort that array by file size, largest first
  3. 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.

Share this post


Link to post
Share on other sites
Melba23

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._______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

 

Share this post


Link to post
Share on other sites
NoTolerance

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 by NoTolerance

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
NoTolerance
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! ;)

Share this post


Link to post
Share on other sites
NoTolerance

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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
NoTolerance

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!

Share this post


Link to post
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
Sign in to follow this  

×