Re: Standard Additions 'read' command - basic questions
Re: Standard Additions 'read' command - basic questions
- Subject: Re: Standard Additions 'read' command - basic questions
- From: Axel Luttgens <email@hidden>
- Date: Mon, 19 Jan 2004 11:49:19 +0100
Chap Harrison wrote:
Hi all,
First post to the list, first-time user of Applescript, but not a
novice programmer.
I used a Unix script to create a text file that I need to read from an
Applescript in order to add records to a Filemaker DB.
The text file consists of records, each of which contains a variable
number of fields. So far so good, with one caveat: the "fields" are
ASCII text which may contain Mac new-line characters (in fact, any
ASCII character). Therefore, I chose to delimit these fields with the
non-ASCII character 0xfe. Furthermore, since each record contains an
unknown number of fields, I must also delimit each record -- I use 0xff.
The first thing I noticed was that a single invocation of 'read'
appears to return the entire *file* as a list!! Is this how 'read' is
supposed to work?
The entire file: if you don't specify any termination condition (as by
using "before", "until"...), the whole file's contents gets read.
As a list: your read command must have included a "using delimiter" part.
So, just to be sure we are speaking about the sames things:
Suppose one has a file containing
aaa0bbb0ccc1ddd0eee0fff1
and R is a reference number to that file (each of following examples
supposes that the file has just been opened for access).
-- Example 1
read R
--> "aaa0bbb0ccc1ddd0eee0fff1"
-- Example 2
read R using delimiter "X"
--> {"aaa0bbb0ccc1ddd0eee0fff1"}
-- Example 3
read R using delimiter "1"
--> {"aaa0bbb0ccc", "ddd0eee0fff"}
-- Example 4
read R until "1"
--> "aaa0bbb0ccc1"
read R until "1"
--> "ddd0eee0fff1"
-- Example 5
read R before "1"
--> "aaa0bbb0ccc"
read R before "1"
--> "ddd0eee0fff"
-- Example 6
read R using delimiter "0" before "1"
--> {"aaa", "bbb", "ccc"}
read R using delimiter "0" before "1"
--> {"ddd", "eee", "fff"}
Seems that the last example could be the right thing for your purpose:
just replace 0 by your field separator, and "1" by your record ending mark.
But...
I tried with ascii character 163 (a sterling pound); it works in a
"before" (or 'until") clause, but not in a "using delimiter" one.
An ascii character 164 (a paragraph character) doesn't work in both
contexts.
So, I guess you encoutered similar problems with your ascii character
254 and 255 as well.
On the other hand, this works as expected:
set D to ascii character 163
set L to "aaa" & D & "bbb"
set AppleScript's text item delimiters to D
get text items of L
--> {"aaa", "bbb"}
There must really be a problem with the "read" command as soon as
characters in the range 128-255 are involved within the various clauses.
Perhaps could you try, as a workaround, to use some other characters as
field separators and record end marks, for example in the range 0-31
(but you seem to say that your data may contain any us-ascii character...)?
If so, are there any limits as to the length of a list?
There was a long-standing problem for the creation of lists with more
than about 4000 items under certain circumstances.
But this has been recently corrected.
So, if you are working with Panther, your version of AppleScript should
include that correction.
The second problem was that 'read' doesn't care for my high-end
delimiters:
set foo to (read fid using delimiter (ASCII character 254)) -- also
tried 255
foo is a list containing one string, which is the contents of the
entire file, with my oddball delimiters intact.
See above.
Are 0xfe or 0xff not valid delimiters as far as AS is concerned?
If you mean the characters having codes 254 and 255, they should.
Well, I allways thought so...
Suggestions for a better approach?
If your files are not too big (some megs or so), you could read them as
a whole in strings and split them into chunks using the TIDs method.
But I wouldn't say it is a better approach, as it goes against the idea
of handling large chunks of data without stressing memory demands (as
"read... until..." allows to do).
HTH,
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.