Re: Total Newbie question
Re: Total Newbie question
- Subject: Re: Total Newbie question
- From: JollyRoger <email@hidden>
- Date: Fri, 15 Jun 2001 08:13:28 -0500
On 6/14/2001 11:25 AM, "roblef" <email@hidden> wrote:
>
I'm looking for a way to clean up text files, from form submissions on the
>
web. With a simple form, we get something like the following:
>
>
name=joe
>
date=january
>
comment=I hate forms
>
>
I'd like to be able to reformat this stuff, either into a spreadsheet or
>
table format, possibly several submissions in a document. Take the <category
>
=xxxxx> out of it altogether. Is this possible with Applescript?
Hi Rob,
Yes, it definitely is possible. Here are the steps you might take:
-- begin script
set myData to "name=joe
date=january
comment=I hate forms"
-- set AppleScript's text item delimiters to {return}
set saveDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
-- coerce the data to text items
-- which gives you a list of "key=value" pairs
set dataList to the text items of myData
-- restore AppleScript's text item delimiters
set AppleScript's text item delimiters to saveDelims
log dataList
-- to separate and work with the key/value pairs,
-- just do something like this:
-- repeat the following on each item of the list
repeat with nextItem in dataList
-- set AppleScript's text item delimiters to {"="}
set saveDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"="}
-- coerce next item to text items
-- which gives you a two item list: {key, value}
set keyValuePair to the text items of nextItem
-- restore AppleScript's text item delimiters
set AppleScript's text item delimiters to saveDelims
-- work with the key value pair as a list:
set myKey to item 1 of keyValuePair
set myValue to item 2 of keyValuePair
log myKey
log myValue
end repeat
-- end script
Here are the results I obtained:
{"name=joe ", "date=january ", "comment=I hate forms"}
"name"
"joe "
"date"
"january "
"comment"
"I hate forms"
HTH
JR