AppleScript - Alternatives for associative array?

Discussion in 'Mac Programming' started by cjoy, Aug 14, 2013.

  1. macrumors member

    Oct 24, 2008
    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

    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:

    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 :)
  2. macrumors 604

    Aug 9, 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:

    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.
  3. thread starter macrumors member

    Oct 24, 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 :p), then fetch the result as a list again.
  4. macrumors 6502

    May 29, 2011
    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.

Share This Page