Re: How to use delimiters in Standard Additions file read/write
Re: How to use delimiters in Standard Additions file read/write
- Subject: Re: How to use delimiters in Standard Additions file read/write
- From: Chris Nebel <email@hidden>
- Date: Tue, 20 Mar 2001 17:52:39 -0800
- Organization: Apple Computer, Inc.
Yow! A lot of different problems with read/write being discussed here -- let
me attempt to tackle all of them. Read/write was totally rewritten in
AppleScript 1.6 and works a lot better, though there are a couple of known
bugs.
First, what's the same:
- There is no difference between "using delimiter" and "using delimiters" --
the compiler considers them to be the same thing and will always remove the
trailing "s".
- delimiters and the parameters for "before" and "until" are assumed to be
one-character strings. (One-byte, really -- sorry, Asian users!) If you use
a longer string, the later characters will simply be ignored.
- You may specify at most one of "to", "for", "before", or "until".
- Using "before", "until", or "using delimiters" will treat the file as text
-- if you specify an "as" type, it will take the appropriate chunk of text
and try to coerce it to the desired type. For example:
-- f contains "19 23 42"
read file "f" as integer using delimiter " " --> {19, 23, 42}
Otherwise, the file is treated as binary data.
What's different:
- Pre-1.6, delimiter lists could be at most 2 items long; in 1.6, they can be
any length. The dictionary still says 2 items max, but it lies.
- Pre-1.6 requires that if you use "using delimiters", you must also have an
"as" parameter. 1.6 removes this requirement; if you omit the "as", it
assumes "as text".
- If you use "as list" pre-1.6, you may not use *any* of the positioning
parameters: "from", "to", etc. The command always reads from the current
position to the end of the file. 1.6 removes this restriction, though you'll
probably want to avoid "as list" for text files because of a bug (see
below). If you're reading a binary list (i.e. "read as list" without using
delimiters), you can't use "to" or "for", because the list data itself tells
you how much to read.
- Pre-1.6, you couldn't write a record or a list that contained other lists
"as record" or "as list" (i.e., as binary data) and then read it back
successfully. A related bug wouldn't let you read more than one binary list
or record from the same file -- the first one would read all the remaining
data. Both of these work in 1.6.
What's broken:
- "as list" combined with "using delimiter" does the wrong thing -- it
returns a list of one-item lists, each of which contains the string you
should have gotten. Using "as text" instead will return the right thing with
either version. For example:
-- f contains "This is a test"
read file "f" as list using delimiter " "
--> {"This", "is", "a", "test"} on 1.4; {{"This"}, {"is"}, {"a"},
{"test"}} on 1.6
read file "f" as text using delimiter " "
--> {"This", "is", "a", "test"}
- Using "as <type>" with "before" or "until" and *no* delimiter will return
garbage or a "can't coerce" error. To work around this, read without the
"as" and then do the coercion in AppleScript. For example:
-- f contains "23 42"
read file "f" before " " as integer --> Can't make some data into the
expected type. (wrong!)
read file "f" before " "
the result as integer --> 23 (right!)
I know what the problem is for both bugs, and they're simple, so they'll get
fixed in the next revision. Also, I won't be gutting and rewriting
read/write again, so there won't be good opportunities to introduce more
bugs. ;)
--Chris Nebel
AppleScript Engineering