Re: Why won't this parse?
Re: Why won't this parse?
- Subject: Re: Why won't this parse?
- From: John W Baxter <email@hidden>
- Date: Thu, 10 May 2001 23:10:50 -0700
At 9:01 -0700 5/10/2001, David Graham wrote:
>
Here's the script (summary):
>
>
property theData : "alias PWI_AAA email@hidden
>
alias PWI_BBB email@hidden
>
alias PWI_CCC email@hidden
>
note PWI_AAA All San Diego Employees
>
note PWI_BBB All San Bernardino Employees
>
note PWI_CCC All South Bay Employees
>
"
>
>
set aliasList to {}
>
-- parse the nickname file
>
set nickFileEntries to paragraphs of theData as list
>
-- parse each entry and make a list of the aliases
>
repeat with thisEntry in nickFileEntries
>
set thisEntry to thisEntry as text
>
-- parse string into three parts (we only need the first two)
>
set AppleScript's text item delimiters to ASCII number 32 -- space
>
-- grab the first two values of the string
>
set strType to text item 1 of thisEntry
>
set strNickName to text item 2 of thisEntry
>
set AppleScript's text item delimiters to {""}
>
-- make a list of aliases
>
if strType is "alias" then set aliasList to aliasList & strNickName
>
end repeat
>
>
return aliasList
>
>
No matter how I try it I get an error when attempting to reference the
>
second text item. I even tried coercing it to a list but couldn't get the
>
second item of the list either. What the heck is going on here?
Once the setting of the text item delimiters is fixed, for example with
set AppleScript's text item delimiters to {space}
then the problem results from the fact that there is an empty paragraph at
the end of the theData string (between the final return and the ").
When that empty paragraph is being processed, thisEntry is "" which doesn't
have a text item 2.
An easy fix is to remove the final return from the theData string...at
which point the script runs and returns
{
"PWI_AAA",
"PWI_BBB",
"PWI_CCC"
}
Perhaps a more robust (and slightly slower) fix is to test for the empty
string each time through the repeat:
property theData : "alias PWI_AAA email@hidden
alias PWI_BBB email@hidden
alias PWI_CCC email@hidden
note PWI_AAA All San Diego Employees
note PWI_BBB All San Bernardino Employees
note PWI_CCC All South Bay Employees
"
set aliasList to {}
-- parse the nickname file
set nickFileEntries to paragraphs of theData as list
-- parse each entry and make a list of the aliases
repeat with thisEntry in nickFileEntries
set thisEntry to thisEntry as text
-- parse string into three parts (we only need the first two)
set AppleScript's text item delimiters to {space}
set entryItems to text items of thisEntry
-- not robust if that list could be LONG
-- grab the first two values of the string
if (count of entryItems) > 1 then
set strType to first item of entryItems
set strNickName to second item of entryItems
end if
set AppleScript's text item delimiters to {""}
-- make a list of aliases
if strType is "alias" then set aliasList to aliasList & strNickName
end repeat
return aliasList
Now you can handle blank strings (or spaceless strings) anywhere in the
data (note I put a blank line into the middle of theData).
But if entryItems could possibly be longer than a few thousand items, do
something else.
--John
--
John Baxter email@hidden Port Ludlow, WA, USA