Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

saving an array after _ArrayPush


  • Please log in to reply
6 replies to this topic

#1 Jury

Jury

    Prodigy

  • Active Members
  • PipPipPip
  • 159 posts

Posted 01 October 2012 - 01:01 PM

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)








#2 water

water

    ?

  • MVPs
  • 15,713 posts

Posted 01 October 2012 - 01:13 PM

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)

UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#3 hannes08

hannes08

    my oh my

  • Active Members
  • PipPipPipPipPipPip
  • 944 posts

Posted 01 October 2012 - 01:13 PM

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

#4 Jury

Jury

    Prodigy

  • Active Members
  • PipPipPip
  • 159 posts

Posted 02 October 2012 - 10:15 AM

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

#5 water

water

    ?

  • MVPs
  • 15,713 posts

Posted 02 October 2012 - 10:41 AM

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)

UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#6 Jury

Jury

    Prodigy

  • Active Members
  • PipPipPip
  • 159 posts

Posted 02 October 2012 - 11:01 AM

Water,

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

Chrres,

Joe

#7 water

water

    ?

  • MVPs
  • 15,713 posts

Posted 02 October 2012 - 11:01 AM

:D
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users