Using grep and awk through shell script, I’ve boiled a very large XML file into a just few numbers I need. Those numbers are inside a single string. Here’s what that string looks like:
0000774155
0000774157
0000774000
0000774140
0000774211
0000772695
Okay, so now how do I turn that single string into an array? Must I loop through offset of “\n” in string?
I often use something like this when using shell scripts:
do shell script "" -- whatever script you're using
set aList to paragraphs of result
For other text, you can use AppleScript’s text item delimiters:
-- Some comma-delimited text
set test to "0000774155,0000774157,0000774000,0000774140,0000774211,0000772695"
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set test to text items of test
set AppleScript's text item delimiters to ASTID
test
Ahhh… paragraphs of… I had seen the previous posts on the ASTID thing, but the single line of code is much better.
I gues I just don’t understand why half of the ASTID script is spent making sure the old delimiters are restored.
Thanks for the quick and on-target help!
ML
Run this in Script Editor (let’s say it’s a script from someone else that you’re not familiar)…
-- Some comma-delimited text
set test to "0000774155,0000774157,0000774000,0000774140,0000774211,0000772695"
set AppleScript's text item delimiters to ","
set test to text items of test
Then run this in Script Editor (let’s say this is your script):
{"a", "b", "c"} as Unicode text
Now, go post to some forum asking why your script is putting commas in the text.
You should (obviously) set the delimiters for any code that relies on it… (The problem above being that that author wasn’t aware that delimiters would affect the coercion. [Or that author wasn’t even aware of delimiters, etc.])
The real problems occur when running scripts in an environment where the delimiters are shared between scripts, such as Script Editor or the Script Menu (where each script has the same instance of the AppleScript object as it’s parent). Basically, restoring the previous delimiters ensures that other (poorly written) scripts in such a context don’t break or otherwise misbehave.