Satimage Previous | Next
Creating and merging lists
Home Documentation Smile Computing AppleScript maths Computing on lists Creating and merging lists  
  • To create an empty list write empty brackets.
    set the_list to {}
  • To add single items to a list, do not use the concatenation operator, use the special properties end and beginning.
    set the_list to {}
    set end of the_list to pi / 10
    set end of the_list to 1.0
    set beginning of the_list to 0.0
      --  {0.0, 0.314159265359, 1.0}
  • To merge two lists use the concatenation operator &.
    set the_list to the_list & {2.0, 3.0}
      --  {0.0, 0.314159265359, 1, 2.0, 3.0}
  • To make a list out of a string, you use a special feature of AppleScript, text items. text items distributes a string into a list of strings according to AppleScript's constant text item delimiters.
    set the_string to "1.0, 2.0, 3.0"
    set text item delimiters to ", "
    set the_list to text items of the_string
      --  {"1.0", "2.0", "3.0"}
    Yet you still do not have a list of numbers, only a list of strings. To make it a list of numbers, apply a neutral operator such as adding 0.
    set the_list to addlist the_list with 0
      --  {1.0, 2.0, 3.0}
  • When the text item delimiters are not enough, a regular expression may be needed. find text accepts regular expressions and can return lists (with the all occurrences option).
    set the_string to "january: 82.4, february: 82.5, march: -132"
    set the_list to find text "-?[0-9]+(\\.[0-9])?" in the_string with string result, regexp and all occurrences
      --  {"82.4", "82.5", "-132"}
    set the_list to addlist the_list with 0
      --  {82.4, 82.5, -132.0}
  • Keep in mind that AppleScript supports two assignment operators, set and copy. When applied to complex types (such as list) copy really clones the data, while set only stores a reference. When you really want a copy of the data, use copy.
    set the_set_list to the_list -- get a reference
    copy the_list to the_copy_list -- copy data
    set item 3 of the_list to 10.0 -- change original list
    get item 3 of the_set_list
      --  10.0
    get item 3 of the_copy_list
      --  1
Version française
Copyright ©2008 Paris, Satimage