Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 14, 2013, 06:03 PM   #1
cjoy
macrumors member
 
Join Date: Oct 2008
AppleScript - Alternatives for associative array?

I need to compare file properties for two sets of files and while this would be a piece of cake with assoc. arrays at hand, I fail to see how to solve this in a halfway decent manner without such an object.

In AppleScript I have lists like these

Code:
filesA = {{filename:"filename X.tif",width:"500"},{filename:"filename Z.tif",width:"200"}, ...}
filesB = {{filename:"filename Z.tif",width:"400"},{filename:"filename v.tif",width:"200"},{filename:"filename X.tif",width:"100"}, ...}
I could iterate over every item of list A and for each item iterate over the entire list b to find matching filenames, but that is horribly inefficient. There ought to be a way to do this in AppleScript that is not a complete PITA, right?

What I aim for is something along these lines (pseudocode), which is obviously not the way to do it in AppleScript:

Code:
filesA = array(
             "filename X.tif" => 500, 
             "filename Z.tif" => 200
         );

filesB = array(
             "filename Z.tif" => 400, 
             "filename V.tif" => 200, 
             "filename X.tif" => 100
         );

foreach (filesA AS key => value)
    ratios[key] = value / filesB[key];
end foreach
Any help much appreciated
cjoy is offline   0 Reply With Quote
Old Aug 15, 2013, 01:22 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
I would probably use AppleScript to write two text files, containing the data from the lists, then process that data using a language that has associative arrays. This assumes you already know such a language, or can learn one with relative ease.


To stay entirely in AppleScript, I'd probably switch each "list of lists" to a "list of records", where each record had a name property, size property, etc. (I assume you can find a doc or tutorial on making AppleScript record objects with arbitrary property names.)

Then I'd probably use a 'whose' clause on the big list to get every item whose name (a property) matched the name I was working on (the iterating object's name). Or if the big list is presumed to have only one item with a given name, then first item whose name matched.


You didn't say what your experience or skill level with AppleScript or other languages is. If you don't know about 'whose' clauses, but do know awk or perl or python, then try the first approach. If you don't know 'whose' clauses and know no other languages, then try the second approach by learning about whose clauses.

Example briefly describing whose clauses:
http://daringfireball.net/2003/09/ap..._whose_clauses

Found by googling applescript whose clause


I wouldn't worry about the speed unless you have 10's of thousands of items in the lists. Since you said nothing about the scale, I'll assume scale isn't an issue, because most people working with something on a huge scale will say so.
chown33 is offline   0 Reply With Quote
Old Aug 15, 2013, 03:37 PM   #3
cjoy
Thread Starter
macrumors member
 
Join Date: Oct 2008
Thank you very much for the assist, I'll look into the "whose approach" for sake of understanding it. I don't think AppleScript has good chances of making it to my favorites though

In the meanwhile I had already resorted to writing each list to a temporary csv file, process them with php (yes - another elegant language there ), then fetch the result as a list again.
cjoy is offline   0 Reply With Quote
Old Aug 15, 2013, 07:35 PM   #4
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
The filter reference form ("whose clause") only applies to application objects - you can't use it with regular AppleScript records or lists.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Programming 0 Oct 29, 2013 12:38 AM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Applications and Mac App Store 0 Oct 29, 2013 12:32 AM
iWeb alternatives are really not alternatives! the7th1 Web Design and Development 30 Dec 8, 2012 09:19 PM
Array Containing Dictionaries ahan.tm iPhone/iPad Programming 2 Nov 18, 2012 11:31 AM

Forum Jump

All times are GMT -5. The time now is 03:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC