Too few list items doesn't error [WAS: Re: String to multiple-item list coercion]
Too few list items doesn't error [WAS: Re: String to multiple-item list coercion]
- Subject: Too few list items doesn't error [WAS: Re: String to multiple-item list coercion]
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 20 Sep 2002 13:33:15 -0700
On 9/20/02 1:35 AM, "Steven Angier" <email@hidden> wrote:
>
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"
Side issue: this exposes a coercion that feels like a bug to me, but I
suppose is intentional.
set ls to {1, 2, 3, 4, 5, 6}
set {a, b, c, d, e} to items of ls
-- {1, 2, 3, 4, 5, 6}
No error that you were setting too few items to the list, unlike the
situation when you set too many:
set ls to {1, 2, 3, 4, 5, 6}
set {a, b, c, d, e, f, g} to items of ls
-- ERROR: Can't get item 7 of {1, 2, 3, 4, 5, 6}
This has real "gotcha" implications, since it also happens with text items
set AppleScript's text item delimiters to {tab}
try
set {a, b, c, d, e} to text items of s
on error
-- some error handler
end try
set AppleScript's text item delimiters to {""}
If you are parsing tab-delimited text paragraphs. and your source had an
unexpected extra carriage return in some field, you'll have two lines which
are too short (perhaps 3 fields each instead of one line with 5 fields), and
the try/error block above will catch it.
But if there was an unexpected tab character in one of the fields, you will
have too many fields (6), and the try/error block _won't_ catch it. Instead,
you have misaligned fields, which might error the script further below, not
in a try block.
For example, in Entourage, it is "impossible" to have tab characters in any
field - tabs usually bring you to the next field, and in one case only - the
notes - enter 3 or 4 spaces rather than a tab character. But "syncing" to
Palm or importing from another program can introduce rogue tab and CR
characters where they don't belong. When exporting later by script, this can
lead to trouble since the sort of try/error block above doesn't catch them.
I would prefer that too few items set to a list errored the same way that
too many items does, instead of being coerced to a subset of the list.
--
Paul Berkowitz
_______________________________________________
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.