Jump to content

Help with multidimension array


bourny
 Share

Recommended Posts

I am tying yo read the contents of a directory into an array which is easy to do. My issue is when I decide I would like to categorise this into a muntidimensional array by alpha. E.G - If the directory found has the letter A as the first character it places it into Row 1 of the array into column 1 - If the next directory is an A it will pop this into column 2 etc - Same with A-Z.

I struggle when I display my array - it seems to have nothing in it. I am guessing _arrayinsert or _arrayadd are not working with multiD arrays.

Here is my code...

*****************

#include <array.au3>

Global $SlidesArray[26][10]

Dim $PosInArray

; Shows the filenames of all files in the current directory.

$search = FileFindFirstFile("c:\ympresenter\vm\custom\*.*")

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

If $File = ".." Then ContinueLoop

If $File = "." Then ContinueLoop

If $File = "" Then ContinueLoop

$Chr = StringLeft($File, 1)

$Asc = AscW ( $Chr )

$FirstChar = $Asc - 64

_ArrayAdd( $SlidesArray[$FirstChar][2], $file)

;_ArrayInsert( $SlidesArray[1][1], 1, "test")

WEnd

; Close the search handle

FileClose($search)

_ArrayDisplay($slidesArray)

Link to comment
Share on other sites

Hi,

If you want a simple "Add" function, you may be better adding to a Dictionary object, as in AutoIt is not so easy to retrieve from Array of Arrays;

best, Randall

; DirSortAlpha.au3
#include <array.au3>

Local $SlidesArray1D[26], $SlidesArray2D[1][26]
For $i = 0 To 25
    $SlidesArray1D[$i] = ObjCreate("Scripting.Dictionary") ; create array of Dict Objects rather than of arrays
Next
Local $PosInArray, $sPath = @ScriptDir
; Shows the filenames of all files in the current directory.
;~ $search = FileFindFirstFile("c:\ympresenter\vm\custom\*.*")
$search = FileFindFirstFile(@ScriptDir & "\*.*")

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $Chr = StringUpper(StringLeft($file, 1))
    $Asc = AscW($Chr)
    $FirstChar = $Asc - 64
    If ($FirstChar > 0) And ($FirstChar <= 26) Then $SlidesArray1D[$FirstChar-1].Add ($file, $file)
WEnd

; Close the search handle
FileClose($search)
; Retrieve the Arrrays from DictObject to 2D Array
For $i = 0 To 25
    Local $iSize = UBound($SlidesArray2D)
    If $SlidesArray1D[$i].Count + 1 > $iSize Then $iSize = $SlidesArray1D[$i].Count + 1
    ReDim $SlidesArray2D[$iSize][26]
    $j = 0
    For $item In $SlidesArray1D[$i].Keys
        $SlidesArray2D[$j][$i] = $item
        $j += 1
    Next
    $SlidesArray2D[0 ][$i] = $SlidesArray1D[$i].Count
Next
_ArrayDisplay($SlidesArray2D,"$SlidesArray2D",1)
Edited by randallc
Link to comment
Share on other sites

Thanks....

This works well and gives me a 2d array with all the names of directories in Alphabetical order. I will need to read thru it and figure out how it works so I can integrate it into my script. (Not used com objects before)

Am I right in saying my original method cannot be done, populating values into a 2d array from variables gathered in a findNextFile loop.... Is this where I was going wrong.???

I like your script but still a bit confused how my method did not work ... I am not that good with arrays as you can tell.

Thanks

Link to comment
Share on other sites

I am tying yo read the contents of a directory into an array which is easy to do. My issue is when I decide I would like to categorise this into a muntidimensional array by alpha. E.G - If the directory found has the letter A as the first character it places it into Row 1 of the array into column 1 - If the next directory is an A it will pop this into column 2 etc - Same with A-Z.

I struggle when I display my array - it seems to have nothing in it. I am guessing _arrayinsert or _arrayadd are not working with multiD arrays.

Here is my code...

*****************

#include <array.au3>

Global $SlidesArray[26][10]

Dim $PosInArray

; Shows the filenames of all files in the current directory.

$search = FileFindFirstFile("c:\ympresenter\vm\custom\*.*")

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

If $File = ".." Then ContinueLoop

If $File = "." Then ContinueLoop

If $File = "" Then ContinueLoop

$Chr = StringLeft($File, 1)

$Asc = AscW ( $Chr )

$FirstChar = $Asc - 64

_ArrayAdd( $SlidesArray[$FirstChar][2], $file)

;_ArrayInsert( $SlidesArray[1][1], 1, "test")

WEnd

; Close the search handle

FileClose($search)

_ArrayDisplay($slidesArray)

The Array.ua3 udf is just for 1-dimensional arrays.

If you want to set a value in element [1][5] all you have to do is

$StringArray[1][5] = $file

Just use the array element exactly like any other variable.

You will need to add a counter for each time you add an entry so that you know the next element to use. A convention often used with arrays is to use the first element ([0]) for this value. So in your case you could say for the letter j

$StringArray[10][0] = 0; to start with, ie no entries.

Then the next J entry is

$StringArray[10][0] += 1;update the count

$StringArray[10][$StringArray[10][0]] = $nextJfile

If the count gets more than 10 you can use ReDim.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...