Re: Understanding a repeat loop (strings/characters)
Re: Understanding a repeat loop (strings/characters)
- Subject: Re: Understanding a repeat loop (strings/characters)
- From: Paul Skinner <email@hidden>
- Date: Sun, 23 Feb 2003 01:56:14 -0500
On Saturday, February 22, 2003, at 08:35 PM, has wrote:
Paul Skinner wrote:
> I'm trying to understand what is happening here:
>
> set theString to (call method "getThing")
> log (class of theString as string) -- "Unicode text"
> repeat with theChar in theString
> -- do things with each character
> end repeat
>
> The "repeat with" loops once for each character of theString, and
> that's what I want to happen. But, since theString is not a list,
can
> somebody explain why the loop variable is fed each character rather
> than the entire string?
>
> Jeff
Because your use of the 'repeat with foo in bar' structure implies
that
bar is a list that AS can repeat through. It's not; but certain data
types can be coerced to a list; so AS does this silently.
Incorrect. The 'repeat with itemRef in val' structure iterates over an
object's items. Both strings and lists have 'item' elements, so either
may be used here. And since in strings 'item' is synonymous with
'character', what you end up with is a reference to each character.
has
Not according to the ASLG...
Repeat With (loopVariable) In (list)
In the Repeat With (loopVariable) In (list) form of the Repeat
statement, the loop variable is a reference to an item in a list. The
number of iterations is equal to the number of items in the list. In
the first iteration, the value of the variable is item 1 of list (where
list is the list you specified in the first line of the statement), in
the second iteration, its value is item 2 of list, and so on.
SYNTAX
repeat with loopVariable in list
[ statement ]...
end [ repeat ]
where loopVariable is any previously defined variable or a new variable
you define in the Repeat statement (see Notes).
list is a list or a reference (such as words 1 thru 5) whose value is a
list. list can also be a record; AppleScript coerces the record to a
list (see Notes).
Statement is any AppleScript statement.
You are correct in your statement that it iterates over the items in
the object.
repeat with x in "abc"
x -->obj { form:'indx', want:'cobj', seld:1, from:"abc" }
end repeat
But it must receive a list or something it can coerce to a list...
Paul Skinner
_______________________________________________
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.