Jump to content

FileReadToArray parameter count?


Zein
 Share

Recommended Posts

#include "..\Include\Array.au3"
#include "..\Include\File.au3"
#include "..\Include\AutoItConstants.au3"

Local $aRetArray
Local $sFilePath = "n.csv"

 _FileReadToArray($sFilePath, $aRetArray, ",")
; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")
 _ArrayDisplay($aRetArray, "Original", Default, 8)

The above code shows two versions of _FileReadToArray and both don't work as expected.
The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

I looked again at the documentation:
 

#include <File.au3>
_FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )


And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

 

Link to comment
Share on other sites

  • Moderators

Your call to _FileReadToArray in the code above has the wrong parameter in the third position. Try 

_FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")

As for it returning a 2D, if you could post an example of the csv that would help greatly to determine whether you need to use the $FRTA_INTARRRAYS parameter.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Sounds like you're using an older version of AutoIt.

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

Link to comment
Share on other sites

16 minutes ago, JLogan3o13 said:

Your call to _FileReadToArray in the code above has the wrong parameter in the third position. Try 

_FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")

As for it returning a 2D, if you could post an example of the csv that would help greatly to determine whether you need to use the $FRTA_INTARRRAYS parameter.

No matter what I do, having anything between $aRetArray and "," gives me an error.
This is a sample csv.

q/b,1,2,ST,1-MAN,9,p,
q/b,1,2,ST,1-MAN,9,p,
q/b-a,1,2,WD,1-MAN,2,p,
q/b-a,1,2,WD,1-MAN,6,p,
q/b,1,2,ST,1-MAN,1,p,
q/b-a,1,2,FF,1-MAN,7,p,
z/b-a,1,2,FF,1-MAN,2,p,
z/b-a,1,2,FF,1-MAN,6,p,
z/b-a,1,3,FF,1-MAN,2,p,

 

 

15 minutes ago, BrewManNH said:

Sounds like you're using an older version of AutoIt.

I'm not working on a computer I can update it very easily on, but all my folders (Aut2Exe, AutoItX, Examples, Include, SciTE, etc.) are all dated 7/11/2018. The rest of the files in my AutoIt3 folder are dated back to 2013 besides the uninstall.exe which is also 7/11/2018.

Does that sound like too old a version of AutoIT to use _FileReadToArray?


Looked into the files and they're actually all from  2013. I'll work on getting it updated to the most recent version. Might not be able to though. Would that potentially break any of the code I'm currently using without issue? 

Edited by Zein
Link to comment
Share on other sites

  • Developers

This works fine for me:

#include "Array.au3"
#include "File.au3"
#include "AutoItConstants.au3"

Local $aRetArray[1000]
Local $sFilePath = "n.csv"

$rc=_FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc = ' & $rc & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_ArrayDisplay($aRetArray, "Original", Default, 8)

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

1 hour ago, Jos said:

This works fine for me:

#include "Array.au3"
#include "File.au3"
#include "AutoItConstants.au3"

Local $aRetArray[1000]
Local $sFilePath = "n.csv"

$rc=_FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc = ' & $rc & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_ArrayDisplay($aRetArray, "Original", Default, 8)

Jos

Gives me the same error. I'll get my autoit updated.

Link to comment
Share on other sites

On 8/14/2018 at 12:50 PM, Jos said:

This works fine for me:

#include "Array.au3"
#include "File.au3"
#include "AutoItConstants.au3"

Local $aRetArray[1000]
Local $sFilePath = "n.csv"

$rc=_FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc = ' & $rc & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_ArrayDisplay($aRetArray, "Original", Default, 8)

Jos

Everything's updated. I keep getting $rc = 0 and then _ArrayDisplay says no array is being passed into it. 

Link to comment
Share on other sites

  • Developers

Ok, so my posted script gives you the clue why this (different data than posted earlier) is giving you problems:

Quote

@@ Debug(9) : $rc = 0
>Error code: 3

This means that _FileReadToArray() returns 0 and Error 3 meaning according the helpfile

Quote

Return Value

Success: 1 and $vReturn holds an array.
Failure: 0, @error flag set to non-zero and $vReturn is set to 0.
@error: 1 - Error opening specified file
2 - Unable to split the file
3 - File lines have different numbers of fields (only if $FRTA_INTARRAYS flag not set)
4 - No delimiters found (only if $FRTA_INTARRAYS flag not set)

Which is indeed the case when you look at your file!

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

This example will create an 2D array with variable row lengths from a file.
I would be surprised if this didn't run on older AutoIt versions.

#include <array.au3>

Local $sFileName = "test.csv"

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

$a1DArray = FileReadToArray($sFileName) ; Create 1D array
;_ArrayDisplay($a1DArray)

Local $a2DArray = _Array1DToVariableRow2D($a1DArray, "/-.,") ; Note: 1 or many row delimiters.
ConsoleWrite(StringFormat("Time taken: %.3f secs", TimerDiff($hTimer) / 1000) & @CRLF)

_ArrayDisplay($a2DArray)


; Parameter - "$a" is a 1D array.
; Output is a 2D array
;
Func _Array1DToVariableRow2D($a, $sRowDelimiter = ",")
    Local $iNumRows = UBound($a)
    Local $aRetArray[$iNumRows][300] ; For really large 2D array to be ReDim latter when the maximum number of columns is found.
    Local $MCol ; Max number of columns from all rows.

    For $r = 0 To $iNumRows - 1
        $aTemp = StringSplit($a[$r], $sRowDelimiter, 2)
        If UBound($aTemp) > $MCol Then
            $MCol = UBound($aTemp)
            If $MCol > UBound($aRetArray) Then ReDim $aRetArray[UBound($aRetArray)][UBound($aRetArray, 2) + 300]
        EndIf
        For $c = 0 To UBound($aTemp) - 1
            $aRetArray[$r][$c] = $aTemp[$c]
        Next
    Next
    ReDim $aRetArray[$iNumRows][$MCol]
    Return $aRetArray
EndFunc   ;==>_Array1DToVariableRow2D

 

Link to comment
Share on other sites

On 8/17/2018 at 5:20 PM, Jos said:

Ok, so my posted script gives you the clue why this (different data than posted earlier) is giving you problems:

This means that _FileReadToArray() returns 0 and Error 3 meaning according the helpfile

Which is indeed the case when you look at your file!

Jos

 

I fixed it and the same error occurs. The csv I gave you was just a stripped version of my actual csv which has some-what private data. And opening my original in Excel shows that the rows are all aligned with the same number of fields. I think the error was actually a blank line at the end of my csv. Which, would yield the error I guess that some rows had different fields.

 

 

On 8/17/2018 at 10:28 PM, Malkey said:

This example will create an 2D array with variable row lengths from a file.
I would be surprised if this didn't run on older AutoIt versions.

#include <array.au3>

Local $sFileName = "test.csv"

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

$a1DArray = FileReadToArray($sFileName) ; Create 1D array
;_ArrayDisplay($a1DArray)

Local $a2DArray = _Array1DToVariableRow2D($a1DArray, "/-.,") ; Note: 1 or many row delimiters.
ConsoleWrite(StringFormat("Time taken: %.3f secs", TimerDiff($hTimer) / 1000) & @CRLF)

_ArrayDisplay($a2DArray)


; Parameter - "$a" is a 1D array.
; Output is a 2D array
;
Func _Array1DToVariableRow2D($a, $sRowDelimiter = ",")
    Local $iNumRows = UBound($a)
    Local $aRetArray[$iNumRows][300] ; For really large 2D array to be ReDim latter when the maximum number of columns is found.
    Local $MCol ; Max number of columns from all rows.

    For $r = 0 To $iNumRows - 1
        $aTemp = StringSplit($a[$r], $sRowDelimiter, 2)
        If UBound($aTemp) > $MCol Then
            $MCol = UBound($aTemp)
            If $MCol > UBound($aRetArray) Then ReDim $aRetArray[UBound($aRetArray)][UBound($aRetArray, 2) + 300]
        EndIf
        For $c = 0 To UBound($aTemp) - 1
            $aRetArray[$r][$c] = $aTemp[$c]
        Next
    Next
    ReDim $aRetArray[$iNumRows][$MCol]
    Return $aRetArray
EndFunc   ;==>_Array1DToVariableRow2D

 

Works without issue! Thank you!

 

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

×
×
  • Create New...