4.3. Adding and Removing Elements in Arrays

Elements in an array can be added:

Example 4-5. Adding elements to an array


  string *names = ({ "joe", "pete" });

  //              -------     --------  
  // names ----> | "joe" | , | "pete" |
  //              -------     -------- 

  names += ({ "jeff" });

  //              -------     --------     -------- 
  // names ----> | "joe" | , | "pete" | , | "jeff" |
  //              -------     --------     -------- 

  names = names + ({ "will" });

  //              -------     --------     --------     -------- 
  // names ----> | "joe" | , | "pete" | , | "jeff" | , | "will" |
  //              -------     --------     --------     --------

The variable names will now be ({ "joe", "pete", "jeff", "will" }).

Notice that when we add an element to an array, it is always appended to the end of the array.

When you append to an array, you're appending the elements of another array. This works for arrays of larger-than-1 size:


  string *names = ({ "joe", "pete" });

  //              -------     --------  
  // names ----> | "joe" | , | "pete" |
  //              -------     -------- 

  names += ({ "jeff", "will" });

  //              -------     --------     --------     -------- 
  // names ----> | "joe" | , | "pete" | , | "jeff" | , | "will" |
  //              -------     --------     --------     --------

Elements in an array can be deleted:

Example 4-6. Deleting elements from an array


  strings *names = ({ "joe", "pete", "tom", "harry" });

  //              -------     --------     -------     ---------
  // names ----> | "joe" | , | "pete" | , | "tom" | , | "harry" |
  //              -------     --------     -------     ---------

  names -= ({ "joe" });

  //              --------     -------     ---------
  // names ----> | "pete" | , | "tom" | , | "harry" |
  //              --------     -------     ---------

  names = names - ({ "tom" });

  //              --------     ---------
  // names ----> | "pete" | , | "harry" |
  //              --------     ---------

The variable names will now be ({ "pete", "harry" }).

Notice how when we delete elements from an array, we delete the item from the array--it doesn't matter whether it's at the front, the back, or in the middle.

Adding and deleting work for ints, objects, and other things as well:


  int *ages = ({ 16, 22, 27, 17, 29 });

  //              ----     ----     ----     ----     ----
  // names ----> | 16 | , | 22 | , | 27 | , | 17 | , | 29 |
  //              ----     ----     ----     ----     ----

  ages -= ({ 17 });

  //              ----     ----     ----     ----
  // names ----> | 16 | , | 22 | , | 27 | , | 29 |
  //              ----     ----     ----     ----

  ages = ages + ({ 34 });

  //              ----     ----     ----     ----     ----
  // names ----> | 16 | , | 22 | , | 27 | , | 29 | , | 34 |
  //              ----     ----     ----     ----     ----

You can delete multiple elements from an array in one pass:


  strings *names = ({ "joe", "pete", "tom", "harry" });

  //              -------     --------     -------     ---------
  // names ----> | "joe" | , | "pete" | , | "tom" | , | "harry" |
  //              -------     --------     -------     ---------

  names -= ({ "joe", "tom" });

  //              --------     ---------
  // names ----> | "pete" | , | "harry" |
  //              --------     ---------

Notice how the elements being deleted don't have to be next to each other.