Thursday, February 21, 2013

String Arrays in Vala

String Arrays are simply bunches of strings grouped together

string[] apples = {"red delicious", "granny smith", "macintosh", "gala", "fuji"}

They look like Python lists, but they are not. They often don't work like lists (you cannot slice them), but they work *really* fast. They are useful if you have a data set that is (mostly) can append to it, but I haven't found a way to delete strings from the array without copying everything else into a new array.

Here is a demo program showing how to
  • Create a string array
  • Append a string to an array
  • Match a string in an array
  • Determine the index of the matched string
  • Retrieve a string from the array (non-destructively)
  • Replace a string with another withing an array
  • Concatenate the array into a single string (for printing)

// string_array.vala

// If a in b
void if_a_in_b ( string a, string[] b, string b_name )  {
    if (a in b) { 
        stdout.printf("Found %s in %s\n", a, b_name); 
    else {
        stdout.printf("%s not Found in %s\n", a, b_name);

// One way to print an array
void print1 (string[] a, string a_name) {
    stdout.printf("Array %s: ", a_name);
    foreach (string item in a) {
        stdout.printf("%s, ", item);

// Another way to print an array
void print2 (string[] a, string a_name) {
    string a_string = string.joinv(", ", a);
    stdout.printf("Array %s : %s\n", a_name, a_string);

// Index of an item in an array
void index_array (string[] a, string a_name, string match) {
    // Record the index of all matches
    int[] indexes = {};
    int i;
    for (i = 0; i < a.length; i++) {
        if (a[i] == match) {
            indexes += i;
    // Print the results
    if (indexes.length == 0) {
        stdout.printf("Indexing %s: %s not found\n", a_name, match);
    else if (indexes.length == 1) {
        stdout.printf("Indexing %s: %s found at position %d\n", 
        a_name, match, indexes[0]);        
    else if (indexes.length == 2) {
        stdout.printf("Indexing %s: %s found at positions %d and %d\n", 
        a_name, match, indexes[0], indexes[1]);        
    else {
        stdout.printf("Indexing %s: %s found at positions ", 
        a_name, match);
        // Convert ints to a strings
        int j;
        for (j = 0; j < indexes.length; j++) {
            if ( j < (indexes.length - 1)) {
                stdout.printf("%d, ", indexes[j]);
            else {
                stdout.printf("and %d.\n", indexes[j]);

void arrays () {
    // Create two string arrays 
    string[] orange = { "fred", "joe", "allen", "steve" };
    string[] blue   = { "jane", "sam", "ellie", "terri" };

    // Test contents of each array
    if_a_in_b ("fred", orange, "Orange");
    if_a_in_b ("fred", blue, "Blue");

    // Length of an array
    stdout.printf("List length: %i\n", orange.length);

    // Item from an array
    stdout.printf("Orange item #2: %s\n", orange[1]);

    // Replace one string in an array
    blue[2] = "pamela";

    // Determine the index (location) of a string in an array
    index_array(blue, "blue", "pamela");

    // Append one string to an array 
    blue += "stacy";

    // Print an array
    print1(orange, "orange");
    print2(blue, "blue");


// Main
public static void main() {
    arrays ();
    stdout.printf("Hello, World\n");

Compile with a simple valac string_array.vala

1 comment:

linvinus said...

nice, but how to clear string array?
or remove item?