• 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: Reformatting a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reformatting a string


  • Subject: Re: Reformatting a string
  • From: Axel Luttgens <email@hidden>
  • Date: Mon, 30 Jan 2012 14:25:22 +0100

Le 30 janv. 2012 à 12:19, Thomas Fischer a écrit :

> [...]
> repeat with aChar in theChars
> 	copy aChar to the end of outText
> 	#copy character id (id of aChar) to the end of outText
> 	# copy (aChar as text) to the end of outText
> end repeat
> [...]
>
> I get an empty string outText as a result: the aChar copied to the end of the list isn't a character anymore.
> theChars and the list outText look identical, but aren't. Is this a bug or a feature?
> Either of the commented versions works, though.
>
> [...]

Hello Thomas,

Indeed, in the above, the loop variable (aChar) is set to a reference upon each iteration, as described at:

	http://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-128481

One gets the same (non) result by explicitly writing that list of references:

	set theChars to {"J", "u"}
	set outText to {a reference to item 1 of theChars, a reference to item 2 of theChars}
	outText as text
		--> ""

Your commented statements in fact de-reference aChar in the process of evaluating an expression; if all you want is to get the ith character of theChars, the less costly is to just de-reference aChar:

	set inText to "Just a test."
	set outText to {}
	set theChars to every character in inText
	repeat with aChar in theChars
		copy contents of aChar to the end of outText
	end repeat
	outText as text
		--> "Just a test."

An un-official way to do the same:

	set inText to "Just a test."
	set outText to {}
	set theChars to every character in inText
	repeat with aChar in theChars
		copy aChar to the end of outText
	end repeat
	contents of outText as text
		--> "Just a test."

Now, it appears that the list to text coercion silently fails at the first non-coercible item:

	{"a", {x: "b"}, "c", "d"} as text
		--> "a"

Even if not documented, such a behavior could be viewed as a feature; and it would explain why coercing a list of references to text just returns the empty string.

Axel


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden


References: 
 >Rép: Reformatting a string (From: KOENIG Yvan <email@hidden>)
 >Re: Rép: Reformatting a string (From: Thomas Fischer <email@hidden>)

  • Prev by Date: Re: Reformatting a string
  • Next by Date: Re: Keychain script
  • Previous by thread: Re: Reformatting a string
  • Next by thread: Re: Reformatting a string
  • Index(es):
    • Date
    • Thread