Re: String to multiple-item list coercion
Re: String to multiple-item list coercion
- Subject: Re: String to multiple-item list coercion
- From: "Arthur J. Knapp" <email@hidden>
- Date: Fri, 20 Sep 2002 13:56:11 -0400
>
Date: Fri, 20 Sep 2002 18:35:28 +1000
>
From: Steven Angier <email@hidden>
>
Subject: String to multiple-item list coercion
>
I may choose to live under a nice cosy rock, but I just learned that
>
strings can be coerced to multiple-item lists:
>
set {a, b, c, d, e} to "abcdefgh"
>
return {a, b, c, d, e}
>
--> {"a", "b", "c", "d", "e"}
>
Similarly, one can now query a particular item of a string:
>
>
item 5 of "abcdefgh"
>
--> "e"
Yes, this has been the case for a long time. I haven't checked
recently, but this syntax also used to be just slightly faster
than "character X of STRING".
>
The ASLG (v1.3.7) indicates that this shouldn't work. I think that this
>
behaviour is a bug or, at best, a bad idea.
I like it. :)
It allows me to create handlers with a parameter that can be either:
1. A list of strings, or
2. A string, which I treat as a "list" of characters:
on DoSomething(stringlist_or_charstring)
repeat with i from 1 to stringlist_or_charstring's length
set thisString to stringlist_or_charstring's item i
A more practical example:
on TrimFromLeft( s, tokens )
(* tokens == string of character tokens, or
* == list of string tokens
*)
set tokenX to 1
set tokensLen to tokens's length
repeat until ( tokenX > tokensLen )
set token to tokens's item tokenX
--
--> char in string, or string in list
if ( s starts with token ) then
set s to s's text ( token's length + 1 ) thru -1
set tokenX to 1
else
set tokenX to tokenX + 1
end if
end repeat
return s
end TrimFromLeft
TrimFromLeft( "[[(({{4}}))]]", "([{" ) --> "4}}))]]"
TrimFromLeft( " Foo Bar Hello World", {space, "Bar", "Foo"} )
--> "Hello World"
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m
}
_______________________________________________
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.