Arrays

im not as familiar with apple script as i am with BASIC… so let me just show you what i would do in a perfect world which is an AS-BASIC hyrd :smiley:

set array1(99)

for i = 1 to 99
copy (read the file "Macintosh HD:whatever path:file" with dilimenator ".") to array1(i)
next i

any ideas :?:

Mmm… Not sure what are you trying to do…
Eg, are you reading this kind of file?

This is some text. Cool. I like.
Ok!, I don't like it really.

Then get a list of strings delimited by “.”:This is some text
Cool
I like
Ok!, I don’t like it reallyThis would be in applescript:set file_to_read to alias “HD:folder:file.txt”
set contents_of_the_file to read file_to_read
set AppleScript’s text item delimiters to “.”
set contents_of_the_file to text items of contents_of_the_file
set AppleScript’s text item delimiters to {“”} → reset delimiters to default
Or, if you wish only the 99th first items:set contents_of_the_file to text items 1 thru 99 of contents_of_the_fileThis would be the quick way… If I copy the statemets you post, it would be anything such as:set my_array to {}
repeat with i from 1 to 99
set end of my_array to "Hello, I’m item " & i
end repeatWhile you don’t find a guy who knows a little of BASIC, perhaps you can post what you’re trying to do (eg, “I wish a list of paragraphs in a text file”) :wink:

Of course, if that’s what you’re trying to do, the AppleScript Way? would be:

set theFileContents to read file “path:to:filename”
set theParas to every paragraph of theFileContents

or maybe:

set theParas to paragraphs 1 through 99 of theFileContents
(assuming, of course, there are at least 99 paragraphs in the file).

The very nice thing about this approach is its portability. If some language uses some other delimiter for paragraph breaks, AppleScript will still handle it based on the system localization.

The other thing that many people forget is that the standard read command can deal with delimiters, so you could:

set theList to read file “path:to:file.txt” using delimiters {“.”}

Note the use of a list for the delimiters. You can specify multiple delimiters to break on, e.g. …using delimiters {“.”, return}

hmm… kind of, let me just make it simple

this is BASIC code (should be close enough to applescript for you to get what i mean)



for i=1 to 1000
     let the_array(i) = i*2
next i


In AppleScript, you’d use:

repeat with i from 1 to 1000
  set item i of the_array to (i * 2)
end repeat

However, this will only work if the_array already contains at least 1000 entries. if it doesn’t you’ll get an error “Can’t set item of the_array to ”

So you’d either need to ensure the array has sufficient entries, or include error checking to extend the array if needed. Alternatively, if you just want to create a new array you could:

set the_array to {}
repeat with i from 1 to 1000
set end of the_array to (i * 2)
end repeat

In this case, my AppleScript ensures the array is empty, then I add a new value of(i * 2) to the end of the array.