Jump to content
Sign in to follow this  
MpDredd

Question about _FileWriteFromArray

Recommended Posts

MpDredd

Basically I have an array that is multi dimension that I want to write to a file.

When I run my script I get the following error:

C:\Program Files\AutoIt3\beta\Include\file.au3 (229) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

FileWrite($hFile, $a_Array[$i_Base])

FileWrite($hFile, ^ ERROR

I could just do a:

FileWrite($export, $aArray[$x][0]&","&$aArray[$x][2]....&","&$aArray[$x][12]

But that seems like a lot of work...

Thanks in advance!

Share this post


Link to post
Share on other sites
SmOke_N

_FileWriteFromArray() only accepts 1 dimensional arrays.

If you want to concatenate the string into one large string, you could always just do a for/next loop then FileWrite.

Lets assume for a second, that each pass of the loop, you want to put on a separate line.

Func _myFileWriteFromArray2DOnly($hFile, $avArray, $bEraseCreate = True)
    If $bEraseCreate Then FileClose(FileOpen($hFile, 2))
    Local $sHoldString = ""
    For $x = 1 To UBound($avArray) - 1
        For $i = 1 To UBound($avArray, 2) - 1
            $sHoldString &= $avArray[$x][$i]
        Next
        $sHoldString &= @CRLF
    Next
    Return FileWrite($hFile, StringTrimRight($sHoldString, 2))
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
MpDredd

I am trying to create a CSV from within the elements. looks like I will just have to go with Plan "B"

Thanks for the help Sm0Ke_N!

Share this post


Link to post
Share on other sites
SmOke_N

I am trying to create a CSV from within the elements. looks like I will just have to go with Plan "B"

Thanks for the help Sm0Ke_N!

Well, I did forget something to help you make the CSV comma delimited (Oops).
Func _myCSV2DCreator($hFile, $avArray, $bEraseCreate = True)
    If $bEraseCreate Then FileClose(FileOpen($hFile, 2))
    Local $sHoldString = ""
    For $x = 1 To UBound($avArray) - 1
        For $i = 1 To UBound($avArray, 2) - 1
            $sHoldString &= $avArray[$x][$i] & ","
        Next
        $sHoldString &= @CRLF
    Next
    Return FileWrite($hFile, StringTrimRight($sHoldString, 3))
EndFunc
I meant to put the comma in last time. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
sirjaymz

1 Dimensional Arrays only!? Smoke N, thanks for info, but that's not what I wanted to hear... and Array inherently, by design, is multi-dimensional.

Any recommendations of capturing a $a_array [200] [20] to a file?

Share this post


Link to post
Share on other sites
weaponx

1 Dimensional Arrays only!? Smoke N, thanks for info, but that's not what I wanted to hear... and Array inherently, by design, is multi-dimensional.

Any recommendations of capturing a $a_array [200] [20] to a file?

You take forever to respond. Just looking at the last piece of code Sm0ke_N posted, it uses the first dimension as the "rows" and the second dimension as the "columns". What is the problem?

Share this post


Link to post
Share on other sites
sirjaymz

Thanks for responding. I apologize, I thought I could respond to any post. Is the etiquette to start a new thread?

This is what I came up with based on the help files. Does this look like it should work?

;My array
Global $ArrayGrid_Queue [100] [20]
$ArrayGrid_Queue[0][0] = "Queue Number";Column0 = Queue Number 
$ArrayGrid_Queue[0][1] = "Job Number"; JobNumber
$ArrayGrid_Queue[0][2] = "Job Number Current Status"; JobNumber current Status.
$ArrayGrid_Queue[0][3] = "Number Of Copies"; Number of Copies to Burn.
$ArrayGrid_Queue[0][4] = "ISO/Image File Name"; "ISO/Image File Name"
$ArrayGrid_Queue[0][5] = "Cover File Name"; "Cover File Name"
$ArrayGrid_Queue[0][6] = "Job Priority"; "JobPriority"
$ArrayGrid_Queue[0][7] = "Time Submitted"; Time Stamp Job Submitted by User to Queue
$ArrayGrid_Queue[0][8] = "Time Started"; Time Stamp by Srv of when Job was Started.
$ArrayGrid_Queue[0][9] = "Time Completed"; Time Stamp by Srv of when Job was Completed.
$ArrayGrid_Queue[0][10] = "Write Speed"; 1x,2x,2,4x,4x, etc.
$ArrayGrid_Queue[0][11] = "Verify"; Yes or NO
$ArrayGrid_Queue[0][12] = "Delete Image"; Yes or NO
$ArrayGrid_Queue[0][13] = "Delete Cover"; Yes or NO
$ArrayGrid_Queue[0][14] = "Job Lock"; Meaning, client can not make changes, server has control of this job only.
$ArrayGrid_Queue[0][15] = ""
$ArrayGrid_Queue[0][16] = ""
$ArrayGrid_Queue[0][17] = ""
$ArrayGrid_Queue[0][18] = ""
$ArrayGrid_Queue[0][19] = ""


$Path2QueueFileUD = "C:\"
    Local $File = FileSaveDialog ( "Save Queue", $Path2QueueFileUD, "Queue File (*.ini)", 2+16, "Queue.ini" )
        If Not @error Then
            For $r = 0 to UBound($ArrayGrid_Queue,1) - 1
                For $c = 0 to UBound($ArrayGrid_Queue,2) - 1
                    IniWrite ( $File, $r, $ArrayGrid_Queue[0][$c], $ArrayGrid_Queue[$r][$c]
                Next
            Next
        Local $szDrive, $szDir, $szFName, $szExt
        _PathSplit ($File, $szDrive, $szDir, $szFName, $szExt)
        $Path2QueueFileUD = $szDrive & $szDir
        EndIf
  • Like 1

Share this post


Link to post
Share on other sites
weaponx

It doesn't look bad but at the top it says you are trying to write a comma seperated file (Post #3), now you are trying to write to an ini. Which is it?

Edited by weaponx

Share this post


Link to post
Share on other sites
Champak

I keep getting an empty file when trying to use this. I know the function is "basically" working, because if I replace the return filewrite part with a basic string ("blah blah blah") it will write that to the file. And I know there is something in my 2D array, because I have _arraydisplay showing me it before and after this function is called. I'm calling it with a gui button msg like:

_myFileWriteFromArray2DOnly($hFile, $avArray)

The $hFile is declared with the path to the file and everything, and the array is global.

Is that wrong? What am I doing wrong?

Share this post


Link to post
Share on other sites
SmOke_N

I keep getting an empty file when trying to use this. I know the function is "basically" working, because if I replace the return filewrite part with a basic string ("blah blah blah") it will write that to the file. And I know there is something in my 2D array, because I have _arraydisplay showing me it before and after this function is called. I'm calling it with a gui button msg like:

_myFileWriteFromArray2DOnly($hFile, $avArray)

The $hFile is declared with the path to the file and everything, and the array is global.

Is that wrong? What am I doing wrong?

I didn't see this question yesterday... And without seeing anything but your snippet of the function itself with the parameter, I can only guess that you are using the function(s) incorrectly.

$avArray is a "Local" scope array with that function, so it ignores anything outside of it (such as your global array... So you still have to pass the global array within that parameter).

If you are trying to change the Global var value, it won't change the Global value unless you use ByRef in the function parameter... ie:

_myFileWriteFromArray2DOnly($hFile, ByRef $avArray)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Champak

Thanks but that wasn't the problem. I was just being careless and was calling the wrong array. However, that ByRef would not have made a difference, because when I put the correct array (which is Global I believe since it works all over and in other Funcs) in the call without the ByRef and it worked...go figure.

So here is my next question to you. I started looking at your code, and it just looks complicated(to me), so I started thinking of all the great things you and a few others have taught me :) , and just wanted to make it easier on MY eyes to clearly understand what is happening and came up with the following. And was wondering if there is a specific reason you did it that way instead of this.

;============Case call

_Create2DFile()

;============

Func _Create2DFile()
    FileClose(FileOpen($hFile, 2))
    Local $sHoldString = ""
    For $x = 0 To UBound($aSource) - 1
            $sHoldString &= $aSource[$x][0] & "," & $aSource[$x][1] & @CRLF
    Next
    FileWrite($hFile, $sHoldString)
EndFunc
Edited by Champak

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  

×