Getting the labels of a record (revisited): Part 2 of 2
Getting the labels of a record (revisited): Part 2 of 2
- Subject: Getting the labels of a record (revisited): Part 2 of 2
- From: Kai <email@hidden>
- Date: Sun, 11 May 2003 23:03:39 +0100
In part 1 of this message, I wrote:
>
In view of this, I also tried a variation on the error/tids approach.
Besides being much more compact than the data analysis method suggested
earlier, this offers a significant improvement in speed. (It's about 10
times faster OMM). So far, I've discovered only a couple of really obscure
ways to cause a slight jitter (to the extent that I don't consider any
additional trapping particularly warranted). However, it wouldn't be the
first time I've overlooked the glaringly obvious...
--============================
to getKeys from r with dupes
tell r's class to if it is not record then error "Can't get keys from "
& it & "."
try
e of r
on error r
end try
set {d, {text:r}, text item delimiters} to {dupes, r, "|"}
set {r, text item delimiters} to {r's text items, ""}
set {r, text item delimiters} to {r as string, "\""}
set {r, l} to {r's text items, {}}
repeat with n from 1 to (count r) by 2
set l's end to r's item n
end repeat
set {o, text item delimiters} to {ASCII character 0, "{"}
set {l, text item delimiters} to {(l as string)'s text items, ", "}
set {l, text item delimiters} to {(l as string)'s text items, o}
set {l, text item delimiters} to {(l as string), ":"}
set {l, k, text item delimiters} to {l's text items, {}, o}
repeat with i in l's items 1 thru -2
tell i's text item -1 to if d or it is not in k then set k's end to it
end repeat
set text item delimiters to {""}
k
end getKeys
--============================
-------------------------------------------------------
(Any wrapped lines abutting the left edge of the window
should be reconnected to the end of the previous line)
-------------------------------------------------------
-----------------
-- test record --
-----------------
set r to {subject:"Re: Getting the labels of a record", date:date "6/6",
|time sent|:"14:36:25 pm", list:{list:{number:1576, string:string}},
alias:{path:{name:{disk:{folder:{file:{item:false}}}}}}, file:"Macintosh
HD:Desktop Folder:test", |initial values|:{list:{1, 2, 3, 4}, |final
average|:10 / 4}}
-------------------------
-- test calls & results --
-------------------------
getKeys from r with dupes
--> {"subject", "date", "time sent", "list", "list", "number", "string",
"alias", "path", "name", "disk", "folder", "file", "item", "file", "initial
values", "list", "final average"}
getKeys from r without dupes
--> {"subject", "date", "time sent", "list", "number", "string",
"alias", "path", "name", "disk", "folder", "file", "item", "initial values",
"final average"}
--============================
I realise that concerns have sometimes been expressed here about the
possible fragility of extracting text using forced errors - perhaps because
language variations or future developments might affect the string wrapper
around an exception message. While I haven't yet come across a variation
significant enough to cause a serious problem, the potential risk remains
nonetheless - and I therefore share some of those reservations.
All the same, in the absence of a better way to winkle out text in certain
situations, it seems the forced error continues to provide an eminently
practical (if not officially endorsed/approved) means of extraction.
The very need to adopt such a trick raises an interesting possibility, which
I suppose could be considered a feature/enhancement request. I'm no
technician, but it seems - to a simple country boy like me, at least - that
the core algorithm used in error reporting (the part that effectively
converts the object of an error into a string) might be recruited as an
additional coercion tool.
If this could be achieved without major re-wiring, it may be possible to
introduce a coercion to something like (say) 'quoted text'. Don't try this
at home, kids - but the syntax might look like this:
--------------------------
{foo:"bar"} as quoted text
--> "{foo:\"bar\"}"
--------------------------
This should remove any uncertainty about what is likely to be returned in a
given situation - and would provide a more reliable starting point for
parsing results using regular text manipulation techniques.
As ever, the game at the centre of the pitch is rarely as simple as it may
seem from the sidelines - but just a thought, FWIW...
=====================================================
Oops! - almost forgot the most important bit...
--------------------------
set r to {name:"mice elf"}
--------------------------
getKeys from r with dupes
--> {"name"}
Tee-hee...
--
Kai
_______________________________________________
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.