Re: Getting label from a record (part 1)
Re: Getting label from a record (part 1)
- Subject: Re: Getting label from a record (part 1)
- From: kai <email@hidden>
- Date: Tue, 18 Nov 2003 00:02:18 +0000
on Mon, 17 Nov 2003 11:12:47 -0700, Michelle Steiner wrote:
>
On Nov 17, 2003, at 2:56 AM, EBI Aktivitet wrote:
>
>
> I'm trying to get a list of all the labels in a record. That is, not their
>
> values, but the actual label name.
>
>
Here is a brute force method. I'm sure that someone will come up with
>
something more elegant, but until then, this should suffice.
I'm not sure about more elegant, Michelle - but I'll take a stab at a couple
of more elephant[1] alternatives if you like... ;-)
([1] They're large enough to warrant splitting this message into a couple of
parts - in an attempt to sneak them past the list 'bot.)
>
set my_record to {a:"record 1", b:"record 2", c:"record 3"}
>
set my_count to count my_record
>
try
>
set my_list to my_record as string
>
on error the_message
>
set text item delimiters to {"{"}
>
set the_message to text item 2 of the_message
>
set text item delimiters to {"}"}
>
set the_message to text item 1 of the_message
>
set text item delimiters to {":"}
>
set the_message to text items of the_message
>
set text item delimiters to {""}
>
set the label_list to {}
>
repeat with loop from 1 to my_count
>
copy the last character of item loop of the_message to the end of the
>
label_list
>
end repeat
>
label_list
>
end try
Which correctly returns:
--> {"a", "b", "c"}
As it stands though, that unfortunately doesn't work too well with key
words:
-------------
set r to {art:"r1", bart:"r2", cart:"r3"}
--> {"t", "t", "t"}
-------------
But that's easily fixed, of course. If we change the statement in the repeat
loop to <copy the last *word* of item loop (etc...)>, then we get:
-------------
set r to {art:"r1", bart:"r2", cart:"r3"}
--> {"art", "bart", "cart"}
-------------
Then again, if we're going to get *really* picky, we still might have a
problem with phrases in pipes:
-------------
set r to {|art deco|:"r1", |bart simpson|:"r2", |carte blanche|:"r3"}
--> {"|", "|", "|"}
-------------
There are also various other conditions that may cause problems with such
conversions.
I posted some code a while back, using a similar starting point - but with
some additional trapping for quite a few (though not necessarily all)
situations:
-------------------------------------------------------
(Any wrapped lines abutting the left edge of the window
should be reconnected to the end of the previous line)
-------------------------------------------------------
--============================
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 to dupes
set {text:r} to r
set text item delimiters to "|"
set r to r's text items
set text item delimiters to ""
set r to r as string
set text item delimiters to "\""
set r to r's text items
set l to {}
repeat with n from 1 to (count r) by 2
set l's end to r's item n
end repeat
set o to ASCII character 0
set text item delimiters to "{"
set l to (l as string)'s text items
set text item delimiters to ", "
set l to (l as string)'s text items
set text item delimiters to o
set l to l as string
set text item delimiters to ":"
set l to l's text items
set k to {}
set text item delimiters to 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
set r to {|art deco|:"r1", |bart simpson|:"r2", |carte blanche|:"r3"}
getKeys from r with dupes
--> {"art deco", "bart simpson", "carte blanche"}
--============================
(The 'with/without dupes' option simply retains duplicated labels or not.)
Possibly a more robust method (if somewhat longer and slower) would be the
alternative that I also posted around the same time...
[Please see part 2 of this message]
---
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.