• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Searching for the equivalent of coercing to type "record"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Searching for the equivalent of coercing to type "record"


  • Subject: Re: Searching for the equivalent of coercing to type "record"
  • From: has <email@hidden>
  • Date: Sat, 8 Mar 2003 00:27:34 +0000

Steve Cunningham wrtoe:

> Prior to the run script construction, they're just text. So...
>
>set tagName to "myTag"
>set valuename to "3.1415"
>run script ("{+class usrf;:{\"" & tagName & "\", \"" & valuename &
>"\"}} as record")
>--> {|myTag|:"3.1415"}
>
> For reasons I don't care to waste cycles on; this returns the tag in
>pipes if your tag contains mixed uppercase and lowercase text.

Thanks, Paul. That sucks about the vertical bars, however.

? Shouldn't do. (Unless you're worried about aesthetics.)

I have
realized that since the field names in my text file have spaces in them
also, I will not be able to turn them into record field names after all.

No, AS just puts bars around it. See p44-5 of the ASLG.

Anyway, a big fat "duh" at this point. The answer is far simpler than employing ghastly kludges to turn strings into record keys From your original post (emphasis mine):

***I know the set of possible field names***, but not which will appear in any
given record or in what sequence

Since your keys are known, you can just define the individual records in your code:

======================================================================

on splitText(txt, delim)
set oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set lst to txt's text items
set AppleScript's text item delimiters to oldTIDs
return lst
end splitText

on parseTableRow(tableRow)
set rec to {}
set lst to splitText(tableRow, tab)
repeat with i from 1 to count lst by 2
set theKey to lst's item i
set val to lst's item (i + 1)
-- your record assembling code goes here; eg:
if theKey is "key1" then
set rec to {key1:val} & rec
else if theKey is "key2" then
set rec to {key2:val} & rec
else
error "Unknown key: " & theKey -- trap unknown keys
end if
end repeat
return rec
end parseTableRow

on tableToRecord(theTable)
set resultList to {}
repeat with aRow in theTable's paragraphs
set resultList's end to parseTableRow(aRow's contents)
end repeat
return resultList
end tableToRecord

__ TEST
set theTable to "key1 foo key2 bar
key2 baz key1 fub
key1 biz"
tableToRecord(theTable)
--> {{key2:"bar", key1:"foo"}, {key1:"fub", key2:"baz"}, {key1:"biz"}}

======================================================================

Short on error handling and you'll need to adapt it to use your own key names. But it'll do the job.

has
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
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.

  • Prev by Date: Re: Searching for the equivalent of coercing to type "record"
  • Next by Date: Re: Searching for the equivalent of coercing to type "record"
  • Previous by thread: Re: Searching for the equivalent of coercing to type "record"
  • Next by thread: Re: Searching for the equivalent of coercing to type "record"
  • Index(es):
    • Date
    • Thread