Jump to content
Sign in to follow this  
Crazyd

Read Write Multi-Dimentional Array to file

Recommended Posts

Crazyd

So I've been working on reading and writing an array to file.

Seemingly I've got it working but I was wondering if there was a better way.

So here is the only way I've figured out how to do it. Please let me know if there is a better way.

Dim $MainArray[45][51]
Dim $RecordCount, $MaxRec = 45, $MaxPerArray = 50

ArrayLoad()
ArrayWrite()
Func ArrayLoad()                                                            ; Load the array from File
    Local $ReadArray, $SplitRead, $sFile = @ScriptDir & "\RefUSA.inf", $Message, $Read 
    $Read = _FileReadToArray($sFile, $ReadArray)
    $RecordCount = 0

    if $Read = 1 Then
        if $ReadArray[0] > $MaxRec Then $ReadArray[0] = $MaxRec                 ;Limits File Read to $MaxRec Records

        For $x = 1 to $ReadArray[0]
            $SplitRead = StringSplit($ReadArray[$x], @tab, 1)
            
            if $SplitRead[0] > $MaxPerArray Then $SplitRead[0] = $MaxPerArray   ;Limits File Read to $MaxPerArray Records

            For $Y = 1 to $SplitRead[0]
                $MainArray[$X-1][$Y-1] = $SplitRead[$Y]
            Next
            $RecordCount = $x
        Next
    Else
        MsgBox( 0, "Error Missing File", "Mouse Location and Color File missing or Corrupt")
        ManualLoad()
    EndIf
_ArrayDisplay($MainArray, "$MainArray AFTER _FileReadToArray()")
EndFunc
Func ArrayWrite()
    Local $GFX[$MaxPerArray], $ArrayLine[1], $SplitRead, $sFile = @ScriptDir & "\RefUSA.inf", $Test
    
    for $Y = 0 to $RecordCount-1
    
        For $X = 0 to $MaxPerArray-1
            $GFX[$X] = $MainArray[$Y][$X]
        Next
        
        If $Y = 0 Then
            $ArrayLine[0] = _ArrayToString($GFX,  @TAB)
        Else
            _ArrayAdd($ArrayLine,_ArrayToString($GFX,  @TAB))
        EndIf
    Next
    _FileWriteFromArray( $sFile, $ArrayLine)
    _ArrayDisplay($ArrayLine, "$ArrayLine AFTER _FileWriteFromArray", $RecordCount-1)
EndFunc
Func ManualLoad()
        Global $MainArray[4][51] = [["1280x1024_Server_Com",215, 420, "000000", "FFFFFF", 228, 427, 265, 673, 325, 380, 689, 280, 18, 43, _
        955, 360, 905, 395, "FFFFFF", 943, 356, "F5F2F1", 905, 315, 905, 395, "F5F2F1", 888, 315, "F5F2F1", _
        975, 351, 750, 400, "FFFFFF", 975, 351, "F5F2F1", 870, 282, 750, 400, "FFFFFF", 820, 283, "F5F2F1", _
        927, 353, "3FAA37"], _
        ["1366x768_Server_Com",256, 420, "000000", "FFFFFF", 228, 427, 262, 673, 325, 380, 689, 280, 22, 45, _
        955, 360, 905, 395, "FFFFFF", 943, 356, "F5F2F1", 905, 315, 905, 395, "F5F2F1", 888, 315, "F5F2F1", _
        975, 351, 750, 400, "FFFFFF", 975, 351, "F5F2F1", 870, 282, 750, 400, "FFFFFF", 820, 283, "F5F2F1", _
        975, 351, "3FAA37"], _ 
        ["1366x768_Server_Resi",257, 428, "000000", "FFFFFF", 263, 453, 263, 696, 374, 358, 689, 280, 22, 45, _
        990, 342, 900, 411, "FFFFFF", 990, 342, "F5F2F1", 924,318, 900, 411, "F5F2F1", 924, 318, "F5F2F1", _
        975, 351, 750, 400, "FFFFFF", 975, 351, "F5F2F1",870, 282, 750, 400, "FFFFFF", 820, 283, "F5F2F1", _
        975, 351, "3FAA37"], _
        ["1280x1024_Win7_Com",216, 427, "F8F9FA", "F5F5F5", 226, 432, 265, 673, 325, 380, 689, 280, 18, 43, _
        955, 360, 905, 395, "FFFFFF", 943, 356, "F5F2F1", 905, 315, 905, 395, "F5F2F1", 888, 315, "F5F2F1", _
        927, 353, 750, 400, "FFFFFF", 975, 351, "F5F2F1", 870, 282, 750, 400, "FFFFFF", 820, 283, "F5F2F1", _
        927, 353, "3FAA37"]]
    $RecordCount = 4
EndFunc

Share this post


Link to post
Share on other sites
LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
BrewManNH

If you look at my signature, you'll see a link to a modified version of the Array.au3 file, in that thread, in the first post, there's a modified _FileWriteFromArray that works with 2D arrays that might be helpful to you. The standard _FileWriteFromArray only works for 1D arrays.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.