Sign in to follow this  
Followers 0
The Kandie Man

_ArraytoFile, my first UDF

5 posts in this topic

While creating my search engine i wrote the following UDF. It is the opposite of the _FileReadtoArray function. I felt that since there was a _FileReadtoArray why wouldn't there be a _ArraytoFile function. Well, i was wrong, very wrong. There is no _ArraytoFile function. I therefore felt it was very important that i create one. I seriously think this function should be included in the next build of AutoIt. There is no reason not to and there is already a related function _FileReadtoArray.

The script itself writes one dimensional arrays and only one dimensional arrays to a file. Each element is written as its own line. For those of you that would like to take a look at it but are too lazy to download it here:

;===============================================================================
;
; Function Name:    _ArraytoFile($avArray, $vFilename, $iStartingElement = 0, $iWritemode = 1)
; Description:    Writes a one dimensional array to a file
; Parameter(s):  $a_Array     - Array to write to file
;                  $vFilename     - File to write to (whole path)
;                  $iStartingElement - The first element in the array to start writing to file (in case the first element is the number of elements in the array)Ex $avArrray[0] or $AvArray[1].  Default is 0.
;                  $iWritemode   - The mode to write to the file: 1 to append to end of file and 2 to erase previous contents

; Requirement(s):   None
; Return Value(s):  On Success - Returns 1 and writes the array to file
;                  On Failure - Returns 0
;                   @ERROR = 1 if it isn't an array
;                   @ERROR = 2 if it isn't one dimensional
;                   @ERROR = 3 if the file couldn't be opened
;                   @ERROR = 4 if there was an incorrect writemode value(must be 1 or 2)
; Author(s):        The Kandie Man
;===============================================================================



Func _ArraytoFile(ByRef $avArray, $vFilename, $iStartingElement = 0, $iWritemode = 1)
    
    Local $iElements, $iOpenFileHandle, $iCounter
    
;checks to make sure that there isn't an invalid number in the write mode variable
    If $iWritemode <> 1 And $iWritemode <> 2 Then
        SetError(4)
        Return 0
    EndIf
    
;checks to make sure the array is in fact an array
    If Not IsArray($avArray) Then
        SetError(1)
        Return 0
    EndIf
    
;checks to make sure it is a one dimensional array
    If UBound($avArray, 0) > 1 Then
        SetError(2)
        Return 0
    EndIf
    
;gets the number or elements in the one dimensional array
    $iElements = UBound($avArray, 1) - 1
    
;attempts to open the file and give it a filehandle
    $iOpenFileHandle = FileOpen($vFilename, $iWritemode)
    
;checks to make sure the file opened.
    If $iOpenFileHandle = -1 Then
        SetError(3)
        Return 0
    EndIf
    
;writes the array to file line by line (very fast)
    For $iCounter = $iStartingElement To $iElements
        FileWriteLine($iOpenFileHandle, $avArray[$iCounter])
    Next
    
    FileClose($iOpenFileHandle)
    
    SetError(0)
    Return 1
    
EndFunc  ;==>_ArraytoFile

Feedback would be nice since this is my first UDF. Any suggestion is welcome. Praises and criticism are both welcome. Thanks.

The Kandie Man

You can find more documentation in the attached zip file:

_ArraytoFile.zip


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites



_FileWriteFromArray

Whoopsie!


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


Link to post
Share on other sites

Strange, i don't have that function. When was it released?


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Strange, i don't have that function. When was it released?

Beta 76 - September 23, 2005.

More specifically, UDF v1.37 - September 20, 2005.


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I could have sworn i just updated my beta this past December. Argh. Well my UDF has a lot more error handling :lmao:. It writes until it gets to the end of the array but it does have the write option. Hey, i think it is pretty neat that mine came out similar and i had no idea his even existed. At least it wasn't a complete waste of my time. I used it in my new indexing engine which i just posted btw.

Edit

Now i remember. I reinstalled it on my other windows OS. I haven't booted windows from that drive in a while and i forgot that i hadn't updated it on my primary windows boot drive.

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

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