Re: list to records
Re: list to records
- Subject: Re: list to records
- From: has <email@hidden>
- Date: Thu, 25 Apr 2002 23:24:04 +0100
Joakim Jardenberg
[...]
>
Say I have a list like the one above. It looks very much like a
>
collection of records, don't you think? And records is exactly what I
>
want. What would be the easiest way to change this list into records so
>
I can look up 'H1 of myRecordsThatUsedToBeAList'. The origin is a file
>
that has the contents listed with one item on every row so I can create
>
my records directly from the file if that would be easier.
>
>
My file:
>
>
A1:100
>
B1:200
>
C1:500
1. Read your file into a variable.
2. Trim any extra white space/returns from the beginning/end of this string.
3. Perform a find and replace, replacing returns with the string: "\", "
4. Perform a find and replace, replacing ":" with ":\""
5. Concatenate: "{" & theString & "\"}"
6. Feed the result to Standard Additions' "run script". (Note that "run
script" is a bit slow. If you're using an application like Smile or QXP
that has a "do script" call, this will be faster.)
(You can find ready-to-use handlers for steps 2 & 3 in stringLib on my
site, blah-blah, etc.)
You could also use a non-record-based solution that uses TIDs to find the
key and return the value. Here's a simple object constructor that will
allow you to create an object containing a single method, getValue(), that
will return a value for a given key.
======================================================================
on newRecObj(theString)
script
property _theString : return & theString
on GetValue(theKey)
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return & theKey &
[NO-BREAK]":"
try
set theValue to _theString's second text item's first
[NO-BREAK]paragraph
on error number -1728
error "Can't find key: " & theKey
end try
set AppleScript's text item delimiters to oldTID
theValue
end GetValue
end script
end newRecObj
--TEST CODE
set fileContents to "A1:100
B1:200
C1:500
A3:B
I3:
E2:K"
set myObj to newRecObj(fileContents)
tell myObj to GetValue("A3")
--> "B"
tell myObj to GetValue("I3")
--> ""
======================================================================
This design is case-sensitive and doesn't have a setValue() option, but
these could be added if needed (the former is doable; the latter is easy).
HTH
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta 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.