Re: merge same (beginning) filenames into different variables
Re: merge same (beginning) filenames into different variables
- Subject: Re: merge same (beginning) filenames into different variables
- From: has <email@hidden>
- Date: Thu, 19 May 2005 12:03:19 +0100
Jan-Bultereys wrote:
>What I want to do is merge the files with the same "beginning" (the files are longer than the example) into different variables.
You don't provide enough information for a precise solution. (e.g. What are we looking at: filenames; file contents? If the keys 'test88', 'testKK2', 'test....' are part of a larger block of data, how are they to be extracted? How are you going to use the results?) Still, the following may point you in the general direction:
======================================================================
(* auto-generated library loading code *)
property _Loader : run application "LoaderServer"
----------------------------------------------------------------------
-- DEPENDENCIES
property _Types : missing value
on __load__(loader)
set _Types to loader's loadlib("Types")
end __load__
----------------------------------------------------------------------
__load__(_Loader's makeLoader())
(* main code *)
on extractKey(theValue)
return theValue's word 1 -- extract a value's key [YOUR CODE HERE]
end extractKey
on buildDict(lst)
set dict to _Types's makeDict()
repeat with itemRef in lst
set theValue to itemRef's contents
set theKey to extractKey(theValue)
if dict's itemExists(theKey) then
set end of dict's getItem(theKey) to theValue
else
dict's SetItem(theKey, {theValue})
end if
end repeat
return dict
end buildDict
(* test *)
set lst to {"88 apple", "KK2 orange", "88 pear", "KK2 tree", "A87 mouse", "BC cheese", "A87 lemon", "BC bling", "A89 navy", "B90 blue"}
-- build a dictionary object containing values grouped by key
set dict to buildDict(lst)
-- retrieve all values with a common key
--log dict's getKeys() --> list of all keys
--log dict's getValues() --> list of all values grouped according to key
log dict's getItem("A87") --> {"A87 mouse", "A87 lemon"}
log dict's getItem("B90") --> {"B90 blue"}
======================================================================
If you've not used AppleMods' libraries before, you'll need to download and install AppleMods' Loader system first <http://applemods.sourceforge.net/getstarted.html>. Run the Loader installer, then download and add the Types library to the ASLibraries folder. You can use the LoaderWizard applet to generate the library loading code to paste at the top of your script.
HTH
has
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden