• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Saving to array list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Saving to array list


  • Subject: Re: Saving to array list
  • From: "koenig.yvan" <email@hidden>
  • Date: Thu, 01 Nov 2012 19:36:51 +0100

Le 01/11/2012 à 15:52, Brian Christmas <email@hidden> a écrit :

G'day scripters

I've got a requirement to save a whole years worth of hourly data.

At the moment, the data is saved daily on an hourly basis as a comma delimited array, then the totals for the 24 hours are saved, the file reset, and starts over. I need to store the days date, and the daily array, for every day of the year.

I've found that if I simply try and add each days new {date,array} to the end of the file, it won't work; I have to read the whole of the saved array, add to it, and then write the whole thing back to the file, otherwise getting the array as a list only reads the first days entry.

Am I doing the only thing possible (which is slow), or is there a method of simply adding to the file on a daily basis, without reading/adding/saving the whole array. I would much prefer to stay saving and reading as a list, it's faster than my previous method of saving individual items as a long comma delimited array (but I'd like it faster still).

My present testing script is below, it takes a minute to run. If I could simply add to the file each day, it would take 3 seconds. It's not much time each day, but it's frustrating having to do it this way.

I've attached the appropriate desktop folder and file so anyone interested can try the script out.

Regards

Santa

BTW: I've deliberately left a name extension off the file name, as under Mountain Lion, this locks the blank file, so information can't be altered except by scripting.

property MailManagerFolderPath : ((path to desktop) & "Mail Manager Folder") as text
property PathToDesktop : path to desktop
property MailManagerDesktopFolder : "Mail Manager Folder"
property YearlyTally : ""
property DailyTally : ""
property theZeroedDate : date

set theZeroedDate to (current date) - (time to GMT) - days
set time of theZeroedDate to 0

set theZeroedDate to date ("1-1-2012")
set c to current date
set DailyTally to my ReadFile3(" RunningTally")
repeat 365 times
set the time of theZeroedDate to 0
my mainloop()
set theZeroedDate to theZeroedDate + days
end repeat
set YearlyTally to my ReadFile2("2012 RunningTally")
say (current date) - c

on mainloop()
my writeYearlyTally("2012 RunningTally", theZeroedDate, DailyTally)
end mainloop

on writeYearlyTally(FileName, writeitem1, writeitem2)
set TheFileName to PathToDesktop & MailManagerDesktopFolder & ":" & FileName as text
try
set tempString to my ReadFile2(FileName)
set end of tempString to {writeitem1, writeitem2, return}
set fRef to (open for access file TheFileName with write permission)
set eof fRef to 0
write tempString as list to fRef
close access fRef
on error errmsg
try
close access fRef

end try
display dialog errmsg
end try
end writeYearlyTally

on ReadFile2(TheFileName)
try
set TheFileNameTemp to (MailManagerFolderPath & ":" & TheFileName) as text
set WholeList to read file TheFileNameTemp as list
return WholeList
on error errmsg
return {}
end try
end ReadFile2

on ReadFile3(TheFileName)
try
set TheFileNameTemp to (MailManagerFolderPath & ":" & TheFileName) as text
set WholeList to read file TheFileNameTemp using delimiter ","
return WholeList
on error errmsg
return {}
end try
end ReadFile3




Oops, I sent my answer directly to Brian.


Hello

As you set eof to 0 every time you want to write to the file, you destroy what was saved before.

Here is a writeTo handler working well :


#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeTo(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo

#=====


Yvan KOENIG (VALLAURIS, France) jeudi 1 novembre 2012 16:52:30




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Saving to array list
  • Next by Date: Re: Checking on the existence of a file
  • Previous by thread: Re: Saving to array list
  • Next by thread: Re: Saving to array list
  • Index(es):
    • Date
    • Thread