Re: Reading nested lists from a preference file
Re: Reading nested lists from a preference file
- Subject: Re: Reading nested lists from a preference file
- From: Sun Real <email@hidden>
- Date: Wed, 25 Apr 2001 09:54:39 +1000
Yosuke Ichikawa's message of 24/4/01 3:00 PM contained:
>
...I want the script to work like so;
>
>
set thelist to {"a", "b", {"c", "d", "e"}}
>
item 3 of thelist
>
--> {"c", "d", "e"}
>
>
...I want to keep this nested list...
>
...and I cannot get AS to read it properly.
[snip]
>
In what sort of form should I prepare the preference file?
>
And how should I let AS read it?
The read/write commands have never worked particularly well for complex
data structures like nested lists, or with non-text values like booleans
& numbers in lists. I know from this mailing list that some people use
them this way anyway. I'll suggest these alternatives:
1) Parse your data structure into a string that can be reconstructed
when you read it back. Maybe something like:
set sampleString to "a
b
c d e"
set someParagraphs to paragraphs of sampleString
--> {"a", "b", "c d e"}
This isn't quite back to what you want yet, but it's easy enough to make
a list out of that particular last item:
set someWords to words of item 3 of someParagraphs
--> {"c", "d", "e"}
These are just examples obviously - what suits your actual data will
depend on the data - and they make use of the 'paragraphs' and 'words'
keywords of AS which do not suit every situation.
If this is the case then define appropriate separators for your data and
use the text item delimiters to encode it when writing to the text file
and decode it when reading back.
Using text files like this is also a good way to get yourself to think
about whether the data really does need to be in this form or not. When
storing preferences, key/value pairs can be written so they're easy to
read in a text editor and simple to parse:
showDoofix=true
dronglePosition=24, 36
This is offers an enormous amount of scope and can handle some reasonably
complex data. The encoding & decoding algorithms can share routines and
they are generally very fast to execute.
Though I mainly use the above method myself, text files have some
limitations and so sometimes it's better to...
2) Write the data structures, objects, or anything at all that AS
understands, directly to a compiled script file. This is pretty cool.
Just make a script with the required properties:
property showDoofix : true
property dronglePosition : {24, 36}
And access it using load script for reading and store script for writing
changed prefs:
set myPref to load script alias "Path:to:prefs:file"
set drongPos to myPref's dronglePosition
--> {24, 36}
Change the value:
set myPref's dronglePosition to {1, 24}
Write any changes back to the script file:
store script myPref in alias "Path:to:prefs:file" with replacing yes
This lets you do even more stuff & no manual processing is required. One
drawback is that these files have to be read with AS to see what the
values are - altered values don't display when the script is opened in
Script Editor.
For real flexibility, use 1 or maybe 2 properties that hold everything in
an agreed format. This allows the easy creation of new properties &/or
objects on the fly.
John Delacour covers the latter method on his site, which Arthur J Knapp
has already provided the URL for.
>
Thank you for any input.
Cheers,
Richard Morton
-- Great Lies of the Music Business: "I'm sure it will work"