Remove first item or last item in list. What is syntax?

I am wondering if there is a way to remove the last item in a list with applescript? I am learning perl at the moment and I came across the pop and push perl commands which do just that. Is there an equilivlent for applescript?
Chuckles :smiley:

set item 1 of yourList to {}

and

set item (count of items in yourList) of yourList to {}

Here is another method to eliminate the first item of a list:

set a to {1, 2, 3, 4, 5, 6, 7, 8}
set a to a's (items 2 thru -1)
-->{2, 3, 4, 5, 6, 7, 8}

Or, to remove the last item:

set a to {1, 2, 3, 4, 5, 6, 7, 8}
set a to a's (items 1 thru -2)
-->{1, 2, 3, 4, 5, 6, 7}

thanks I was using a more convoluted method.
:smiley:

Something else to consider:

set a to {1, 2, 3, 4, 5, 6, 7, 8}
set a to rest of a
-->{2, 3, 4, 5, 6, 7, 8}

set a to {1, 2, 3, 4, 5, 6, 7, 8}
set a to reverse of (rest of (reverse of a))
-->{1, 2, 3, 4, 5, 6, 7}

Hi :slight_smile:

You have also, in the “list” class, the properties “rest” and “reverse”:


set myList to {1, 2, 3, 4, 5, 6, 7, 8, 9}

-- To eliminate the first item, you can use the "rest" property:
set myListWithoutFirstItem to rest of myList --> {2, 3, 4, 5, 6, 7, 8, 9}

-- To reverse every items in the list, you can use the "reverse" property:
set myReverseList to reverse of myList --> {9, 8, 7, 6, 5, 4, 3, 2, 1}

-- And, to eliminate the last item, you can to combine this two properties, like this:
set myListWithoutLastItem to reverse of (rest of (reverse of myList)) --> {1, 2, 3, 4, 5, 6, 7, 8}

:wink:

Edit:

Woups, the administrator was faster than me. :wink:

I don’t recommend combining those two and then performing another reverse to complete this task, for a couple of reasons. Let’s look into how AppleScript works for a second.

AppleScript is a verbose front-end for other commands. Many of its commands are groups of commands. When you create a list of items in AppleScript, you’re actually creating a reference to a bunch of objects you’ve specified for your “list”. When you make a change to a list:

set theList to (theList & "another item")

What you’ve actually done is invoked a convenience function that creates a new CFArray with the contents of the old list plus the new item or items. The old list is then destroyed and replaced with the new one. Same goes for:

set item 1 of theList to {}

Let’s say theList is:

“a”,“b”,“c”

When we set the first item to {}, a copy of the list is made with the following items:

{},“b”,“c”

Which results in a two-item list:

“b”,“c”

This becomes the new list, and the old copy is released. What you propose is to:

  1. Create a copy of the list on your own with parts removed. (Essentially, creating a new array out of the original with the first item removed, then using that to create another new array. The first new array is then released. The original is untouched.)

  2. Create a new list (a reverse of the original), resulting in another array constructor operation.

  3. Do TWO array reverses resulting in a list reconstruction.

If you use my methods or Craig’s, you aren’t doing nearly as much work (number of operations OR thinking-wise) and your code winds up cleaner.

If you’re interested in seeing a rough idea of how arrays get reversed programmatically, and not just with the command “reverse”, google for “reverse array” and check out some code examples. Then think about how many times you propose doing that. :wink: