Sign in to follow this  
Followers 0

saving an array after _ArrayPush

7 posts in this topic

#1 ·  Posted

I have searched but can't find anything about how to save an array after doing an _ArrayPush. I'm trying to save the array as a list with one array element to a line. This is what I've tried and it displays my array with Hello world! at the top but doesn't save it to the file.

Thanking you,

Joe

#include <Array.au3>
#include<File.au3>
Dim $echrDone
$echrdonefile = @MyDocumentsDir & '\AutoIt_code\getter\resources\ECHR_done.txt'
$file = FileOpen($echrdonefile, 1)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open " & $echrdonefile)
Exit
EndIf
FileClose($file)
If Not _FileReadToArray($echrdonefile, $echrDone) Then
MsgBox(4096, "Error", " Error reading log to Array   error:" & @error)
Exit
EndIf
$echrDone = _ArrayUnique($echrDone)
_ArrayDisplay($echrDone, "$aRecords")
_ArrayPush($echrDone, "Hello world!", 1)
FileClose($file)
_ArrayDisplay($echrDone2, "$avArrayTarget AFTER _ArrayPush() string to beginning")
_FileWriteFromArray($file, $echrDone2, 1)

Share this post


Link to post
Share on other sites



#2 ·  Posted

This adds the array to the end of the existing file:

#include 
#include 
Dim $echrDone
$echrdonefile = "C:temptest.txt"
$file = FileOpen($echrdonefile, 1)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open " & $echrdonefile)
Exit
EndIf
If Not _FileReadToArray($echrdonefile, $echrDone) Then
MsgBox(4096, "Error", " Error reading log to Array error:" & @error)
Exit
EndIf
$echrDone = _ArrayUnique($echrDone)
_ArrayDisplay($echrDone, "$aRecords")
_ArrayPush($echrDone, "Hello world!", 1)
_ArrayDisplay($echrDone, "$avArrayTarget AFTER _ArrayPush() string to beginning")
_FileWriteFromArray($file, $echrDone, 1)

My UDFs and Tutorials:

UDFs:
Active Directory (NEW 2015-08-07 - Version 1.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (NEW 2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

Share this post


Link to post
Share on other sites

#3 ·  Posted

Hi Joe,

first you can leave out the part with the FileOpen() and FileClose()s :-) Too much code you don't need.

Add an error check to your _FilereadToArray() and FileWriteFromArray() functions. (Get the returncode and @error macro)

Look at the helpfile about _FileWriteFromArray(). Use the @CRLF macro as delimiter.

;)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#4 ·  Posted

Thanks for attempting to help me but the resulting file after _FileWriteFromArray is just the same file as the original file _FileReadToArray - I want to know how to end up with a saved a file that has the added Hello world! as the first line.

Joe

Share this post


Link to post
Share on other sites

#5 ·  Posted

To get the "Hello world" record in the output file you have to write the whole array and start with index = 0. Hence change

_FileWriteFromArray($file, $echrDone, 1)
to
_FileWriteFromArray($file, $echrDone, 0)

My UDFs and Tutorials:

UDFs:
Active Directory (NEW 2015-08-07 - Version 1.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (NEW 2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

Share this post


Link to post
Share on other sites

#6 ·  Posted

Water,

That's the trick! Thanks for that I think I see the logic of this now.

Chrres,

Joe

Share this post


Link to post
Share on other sites

#7 ·  Posted

:D

My UDFs and Tutorials:

UDFs:
Active Directory (NEW 2015-08-07 - Version 1.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (NEW 2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

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