Strings and Arrays

how do you fill a string to an array by each letter?

in basic I would:

dim the_array(32)

let the_name = "BillyBob"

for i = 1 to LEN(the_name)
the_array(i) = MID(the_name,i,1) //note the MID function is in form MID([source], start, length)
next i

(so the mid of the_name when i = 2 is the letter i because its position 2 (start 2) and is 1 character long)

any ideas? its driving me mad trying to learn all these applescript from BASIC :smiley:

Hi,

You don’t use arrays in AppleScript. The closest thing is list. You could write an AppleScript something like your basic but there’s an easier way. Something like this:

set the_string to “The String”
set char_list to text items of the_string

I just added this. This is the closest I can come to your script:

set the_string to “The String”
set the_array to {}
repeat 32 times
set end of the_array to “”
end repeat
repeat with i from 1 to (length of the_string)
set item i of the_array to MidF(the_string, i, 1)
end repeat
the_array

on MidF(the_string, start_point, the_length)
return (text start_point thru (start_point + the_length - 1) of the_string)
end MidF

Note that i didn’t use error checking in the subroutine. You would want to add that kind of stuff.

gl,
Kel.

Thanks Kel!!! :smiley:

that worked perfect, your a genious!

An interesting concept in AppleScript is “text item delimiters”, which means “item which separates text units”.
By default, applescript’s text item delimiters are {“”} (list of empty string, or “no character”). So, by default:

text items of "hello"

will return a list containing every text item of “hello” → {“h”, “e”, “l”, “l”, “o”}
Also, AppleScript defines a property length for some classes, such as lists or strings… That means that a list or string contains items or units. So:

items of "Hello"

will also return → {“h”, “e”, “l”, “l”, “o”}
Of course, you can count these items. So, if you wish a list of 32 items when you provide a string with less than 32 characters:

set the_list to items of "Hello"
repeat while (the_list's length is not 32)
	set the_list's end to ""
end repeat
-- if you're not sure the initial string will be less than 32 characters
-- you can add the following line:
-- set the_list to items 1 thru 32 of the_list

(just another point of view :wink: )

thanks, i love coding how you can attack these things from any angle

Hi, I was wondering what’s up with the repeat thing? Why you had to do that? Thanks!

Hi Feluks,

I only lightly remember this post, but what I was thinking that when you define an array, it begins with a certain number of poaces in memory. On the other hand, you need to fill add or concatenate items to a list. Actually, this is not so bad because it’s faster adding things to lists after you do something like this. To speed up filling up the list you can use the ‘a reference to’ operator or other methods where you access the list in memory. Something like this:

set the_list to {}
set list_ref to a reference to the_list
repeat 10000 times
set end of list_ref to missing value
end repeat
count the_list

Just one more step beyond.

gl,

Kel, thank you very much for the explaination! :smiley: :smiley:

You’re welcome FeLuks.

If you have Jon’s commands, then here’s a little comparison test between adding items to a list and replacing items in a list.

set the_list to {}
set list_ref to a reference to the_list
set t1 to the ticks
repeat with i from 1 to 10000
set end of list_ref to i
end repeat
set t2 to the ticks
set d1 to (t2 - t1)

set the_list to {}
set list_ref to a reference to the_list
– fill the list
repeat 10000 times
set end of list_ref to missing value
end repeat
– now that you have your filled list (array)
set t1 to the ticks
repeat with i from 1 to 10000
set item i of list_ref to i
end repeat
set t2 to the ticks
set d2 to (t2 - t1)
set the_list to {}
display dialog ("Adding items - " & d1 & return & "Replacing items - " & d2)

gl,

Thanks man, that’s really great. Fine example.

And if you want the answer in milliseconds, replace every instance of “the ticks” with GetMilliSec after having installed an OSAX by that name to be found in here in osaxen.com. GetMilliSec simply returns the number of milliseconds since system startup; has no arguments, but is finer grained than “the ticks”. When I run Kel’s script with GetMilliSec, for example, I get 311 and 34 for the answers or a ratio of 9.147:1; with “the ticks” I get 16 and 2 for a ratio of 8:1