7. Lists

A list is an ordered collection of objects of different type, i.e. scalars, matrices, string variables. You can find all the following examples in 2116matrix09.xpl.


7.1 Creating Lists


l = 2134 list (x1 {,x2{,...}})
generates lists l from given objects (x1 {,x2{,...}})
y = x.elem
gives the element elem of a list x

We create a list with the function 2139 list, which takes as arguments the ordered elements of the list. The commands

  seller1 = list("Ludwig","Beethoven",20)
  seller1
create a list, with the string "Ludwig" as its first element, "Beethoven" as its second element, and the number 20 as its third element. This list is stored in the variable seller1, the content of seller1 is displayed as follows:
  Contents of seller1.el1
  [1,] "Ludwig"
  Contents of seller1.el2
  [1,] "Beethoven"
  Contents of seller1.el3
  [1,]       20
The three components of the list are displayed. The default label of the ith element of the list seller1 is seller1.eli. Thus, the second element of the list seller is labeled as seller1.el2. The dot operator allows us to access any component of the list. We access the second component as
  seller1.el2
  Contents of el2
  [1,] "Beethoven"
However, we could give a label to the components of a list. Consider the following list:
  age = 35
  seller2 = list("Claudio","Arrau",age)
  seller2
Since the third component of the list seller2 is the assigned variable age, this element is labeled age:
  Contents of seller2.el1
  [1,] "Claudio"
  Contents of seller2.el2
  [1,] "Arrau"
  Contents of seller2.age
  [1,]      35

A list can be a component of another list: We define the list sellers, which contains the lists seller1 and seller2:

  sellers = list(seller1,seller2)
  sellers
returns
  Contents of sellers.seller1.el1
  [1,] "Ludwig"
  Contents of sellers.seller1.el2
  [1,] "Beethoven"
  Contents of sellers.seller1.el3
  [1,]       20 
  Contents of sellers.seller2.el1
  [1,] "Claudio"
  Contents of sellers.seller2.el2
  [1,] "Arrau"
  Contents of sellers.seller2.age
  [1,]       35
In that case, we access the element age of the variable seller2 of the list sellers by using twice the dot operator:
  sellers.seller2.age = 36
  sellers.seller2.age
which returns
  Contents of sellers.seller2.age
  [1,]       36


7.2 Handling Lists


2174 insert (l, pos, x)
inserts an object x at the specified position pos within a list l
2177 delete (l, pos)
deletes the object at the given position pos from the list l
2180 append (l, x)
appends an object x to the specified list l
y = x{n}
gives an element of a list x with number n

You may use any kind of object as an element of a list, e. g. strings, matrices or even lists. To insert an object within a list one can use the 2187 insert function.

  name = "Andrew"
  insert(sellers, 1, name)
  sellers
inserts a new element before el1 .This element contains the name "Andrew". Unfortunately, the element is not assigned the object name but the name according to the position notation ( el1 in the example). Because the former first element retains its old name el1 we now have two elements with the same name but different contents.
  Contents of sellers.el1
  [1,] Andrew
  Contents of sellers.el1
  [1,] 1.000000
  Contents of sellers.age
  [1,] 29.000000

Using the ádot operator (seller.el1) we will only get the value of the first object with this name ("Andrew"). We can, however, access the elements of a list not only by its name but also by its number by using the braces ({}) function. So,

  sellers{1}
gives
  Contents of el1
  [1,] "Andrew"

As well as inserting an object we can delete it by using the function 2194 delete.

  delete(sellers, 1)
deletes the second element (sellers.el1="Andrew") from the list. Furthermore, we can 2197 append an object to the end of a list using 2200 append:
  sales = list(100, 200, 75)
  append(sellers, sales)
  sellers
appends the list object sales as a new element to the list object seller:
  Contents of sellers.seller1.el1
  [1,] "Ludwig"
  Contents of sellers.seller1.el2
  [1,] "Beethoven"
  Contents of sellers.seller1.el3
  [1,]       20 
  Contents of sellers.seller2.el1
  [1,] "Claudio"
  Contents of sellers.seller2.el2
  [1,] "Arrau"
  Contents of sellers.seller2.age
  [1,]       36 
  Contents of sellers.sales.el1
  [1,]      100 
  Contents of sellers.sales.el2
  [1,]      200 
  Contents of sellers.sales.el3
  [1,]       75

In contrast to the á2203 insert function the á2206 append function assigns the object name of the appended object to the new element name of the list unless it is only a temporary object (expression). If the first parameter is not a list, i.e. not a composed object, then a list is generated by the 2209 append function, where the first parameter becomes both the name of the resulting list and the name of its first component.


7.3 Getting Information on Lists


2229 names (x)
gives the names of all components of a list object x
y = 2232 size (x)
gives the number of elements of x contained in a list
y = 2235 comp (obj, com)
checks whether an object obj has a specific component com or not

The function 2238 names gives the names of all components of a list object. The resulting object is a vector, such that one can access single values using the square brackets operator [].

  names(sellers)[2:3]
shows
  Contents of names(sellers)[2:3]
  [1,] "seller2"
  [2,] "sales"
Note that the output of the third element of the list gives only the name of the list object sales itself but not the names of its components.

The 2243 size function gives the number of elements that are contained in a list. For example,

  li = list(sellers, matrix(3, 4))
  size(li)
gives
  Contents of numcomp
  [1,] 2
As before, only the elements of the respective list are counted, no matter what type they are or whether they have subobjects.

Finally, 2246 comp checks whether a list contains a certain component. For example

  comp(li,"sellers")
returns
  Contents of comp
  [1,]        1
whereas
  comp(li,"seller1")
returns
  Contents of comp
  [1,]        0
The false value 0 tells us that the list li does not contain a component named seller1.

The three latter lines are the main body of the program. The column vector elements contains the component name to be searched for in the lists given in column vector objects. The comparison itself is done in the self written procedure compare. In the second line of the macro the global list seller is announced to this procedure (otherwise the macro would not know any global object). By looping (using the 2249 while statement), the á2252 comp function is applied to all rows to be compared (corresponding values of the objects and the elements vector). The result is appended to a list named result. Note that before entering the loop result is not a list object. Executing the program yields

  Contents of result.result
  [1,] comparison
  Contents of result.el2
  [1,] -1.000000
  Contents of result.el3
  [1,] 2.000000
  Contents of result.el4
  [1,] 0.000000
The outcome of the first comparison (sales in lis) is 1 since the object "lis" does not exist. If the comparison is true, like in the second case, 2255 comp gives the element number of the component (age) in the list (seller). If the element is not contained in the existing list, the result of 2258 comp is 0 (in the example there is no element name in list seller).



Method and Data Technologies   MD*TECH Method and Data Technologies
  http://www.mdtech.de  mdtech@mdtech.de