Applescript handler, repeat with i from 1 to the number of this_list

Viewed 455

If someone could tell me what I am doing wrong here, or point me in the right direction to find the answer I would be grateful.

I have a list of numbers, and a list of corresponding names

numList {"1", "2", "4", "6"}
nameList {"bob", "joel", "mickey", "tara", "jason", "stacey"}

I am trying to make a handler to take the numbers from numList and get the corresponding names as a list.

The result should be {"bob", "joel", "tara", "stacey"

this is what I have, but it does not return the correct names.

on myFirstHandler(this_list, other_list)
    set rest_list to {}
    set availlist to {}
    repeat with h from 1 to the number of this_list
        set the rest_list to item h of other_list
        set the end of the availlist to rest_list
    end repeat
    return availlist
end myFirstHandler

set numList {"1", "2", "4", "6"}
set nameList {"bob", "joel", "mickey", "tara", "jason", "stacey"}

myFirstHandler(numList, nameList)
set AlcoholAvailable to the result

returns {"bob", "joel", "mickey", "tara"}
1 Answers

You were very, very close with your attempt. Here's a modified, corrected version of it:

on myFirstHandler(this_list, other_list)
    set rest_list to {}

    repeat with h in this_list
        set the end of rest_list to item h of other_list
    end repeat

    return rest_list
end myFirstHandler


set numList to {1, 2, 4, 6}
set nameList to {"bob", "joel", "mickey", "tara", "jason", "stacey"}

myFirstHandler(numList, nameList)
set AlcoholAvailable to the result

The main key difference is the way the repeat loop is defined: in your original attempt, you were looping through the indices of the source list, so you were always going to be utilising numbers that run 1, 2, 3, 4, 5, 6, and therefore, extracting those items from your target list. In my modified version, I'm looping through the values, therefore I'm able to make use of the numbers from the source list 1, 2, 4, 6.

So compare your repeat clause:

repeat with h from 1 to the number of this_list

with mine:

repeat with h in this_list

Both are perfectly valid bits of AppleScript, but do slightly different things: one uses indices, the other uses values.


A slightly more advanced, but ever so more versatile, method of doing it would be like this:

set numList to {1, 2, 4, 6}
set nameList to {"bob", "joel", "mickey", "tara", "jason", "stacey"}

on map(L, function)
    local L, function

    tell result to repeat with x in L
        set x's contents to function's fn(x)
    end repeat

    L
end map

on itemInList(L)
    script
        on fn(x)
            item x of L
        end fn
    end script
end itemInList

map(numList, itemInList(nameList))

Here, I've defined a "basic" mapping function. A mapping function takes a function—in this case, one that returns an element from a target list—and applies that function to each element from an array—in this case, your source list, numList. This list is modified in the process, so you'll find that the elements of numList have been replaced with {"bob", "joel", "tara", "stacey"}.

What makes this construct special is that you can replace the function (handler) that map uses in this example, itemInList() and define other handlers of a similar nature that do other things to your list.

Related