Re: Saving to array list
Re: Saving to array list
- Subject: Re: Saving to array list
- From: Axel Luttgens <email@hidden>
- Date: Fri, 02 Nov 2012 10:06:40 +0100
Le 1 nov. 2012 à 18:11, I wrote :
> Le 1 nov. 2012 à 15:52, Brian Christmas a écrit :
>
>> G'day scripters
>>
>> [...]
>>
>> 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).
>>
>> [...]
>
> Hello Brian,
>
> There may be a (fragile?) way.
>
> [...]
Hello Brian,
I've had the opportunity to adapt your script so as to implement my suggestion (for the sake of clarity, I've removed the parts not directly related to your question).
The idea is thus to first write an empty list to some file.
This yields a file eight bytes long, with following data in hexadecimal:
6c 69 73 74 00 00 00 00
That data may be interpreted as:
bytes 1 to 4: ascii characters 'list'
bytes 5 to 8: 0 encoded as a 32-bit integer
Appending an item to the list contained in the file is then just a matter of:
increment the integer stored in bytes 5 to 8
write the new item, in its binary form, to the end of the file.
HTH,
Axel
property MailManagerFolderPath : ((path to desktop) as text) & "Mail Manager Folder:"
set theZeroedDate to date ("1-1-2012")
set c to current date
initYearlyTally("2012 RunningTally")
set DailyTally to read file (MailManagerFolderPath & " RunningTally") using delimiter ","
repeat 365 times
writeYearlyTally("2012 RunningTally", theZeroedDate, DailyTally)
set theZeroedDate to theZeroedDate + 1 * days
end repeat
set YearlyTally to read file (MailManagerFolderPath & "2012 RunningTally") as list
say (current date) - c
on initYearlyTally(FileName)
local fRef
set fRef to open for access file (my MailManagerFolderPath & FileName) with write permission
set eof fRef to 0
write {} to fRef
close access fRef
end initYearlyTally
on writeYearlyTally(FileName, writeitem1, writeitem2)
local fRef, currListLength
try
set fRef to open for access file (my MailManagerFolderPath & FileName) with write permission
set currListLength to read fRef from 5 to 8 as integer
write (1 + currListLength) to fRef starting at 5
write {writeitem1, writeitem2, return} to fRef starting at (1 + (get eof fRef))
close access fRef
on error errmsg
try
close access fRef
end try
display dialog errmsg
end try
end writeYearlyTally
_______________________________________________
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