• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: String to multiple-item list coercion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: String to multiple-item list coercion
      • From: Steven Angier <email@hidden>
  • Prev by Date: Re: string as key to record
  • Next by Date: Re: The State of the Nation
  • Previous by thread: String to multiple-item list coercion
  • Next by thread: Re: String to multiple-item list coercion
  • Index(es):
    • Date
    • Thread