16.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 38176 XLGmatrix09.xpl .


16.7.1 Creating Lists


l = 38266 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 38271 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 $ i$th element of the list seller1 is seller1.el$ i$. 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


16.7.2 Handling Lists


38436 insert (l, pos, x)
inserts an object x at the specified position pos within a list l
38439 delete (l, pos)
deletes the object at the given position pos from the list l
38442 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 38449 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 38456 delete .

  delete(sellers, 1)
deletes the second element (sellers.el1="Andrew") from the list. Furthermore, we can 38459 append an object to the end of a list using 38462 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   38465 insert function the   38468 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 38471 append function, where the first parameter becomes both the name of the resulting list and the name of its first component.


16.7.3 Getting Information on Lists


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

The function 38645 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 38650 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, 38653 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.