Examples: Searching and Querying Arrays

Examples: Searching and Querying Arrays

Snap includes blocks for searching and querying arrays: you can search an  array  through procedural programming commands, or query it like a table or database through statements containing complex logic. Depending on your needs and programming style, both techniques are useful ways to manage arrays. The following examples give you an overview of both techniques.

Searching Arrays

There are 4 basic types of searches, done through a simple snap block. Each search returns a different data type:

Example of "Any" Search

  • "Any"
    • Result is a boolean. Value is true if any element in the array meet search criteria.
  • "Filter"
    • Result is an array. All elements of the searched array that match the search criteria.
  • "Find"
    • Result is the value of the first element of the array that meets search criteria.
  • Index of
    • Returns the index (number) of the first element in the array with a specific value.

In the above example, a text array called "ArrayOfNames" is declared and loaded with the values of “Mars”, “Jupiter”, “Saturn”. Then a boolean variable is declared and loaded with the result of the "Any" search. The search criteria is: does any element have a value equal to "Earth". No element matches this so the result is false.

Example of "Find" Search

Example of "Filter" Search

Example of "Index Of" Search

In the above example we have the same text array with three planet names. Then a text variable, called "Planet Array" is declared and loaded with the result of the "Find" search. The search criteria is: does any element have a value that contains the text "Ma". One element matches this criteria so it's value, "Mars", is returned.

In the above example we have the same text array with three planet names. Then a text array variable, called "Planet Array" is declared and loaded with the result of the "Filter" search. The search criteria is: does any element have a value that is equal to "Mars". One element matches this criteria so that element is added to the returned array.

In the above example we have the same text array with three planet names. Then a number variable, called "Planet Array Index" is declared and loaded with the result of the "Index Of" search. The index of the element that has the value of "Jupiter" us returned. "Jupiter" is in the second element of the array so it's index of 1 is returned (indexes are 0 based).

Based on a different technique and different Snap blocks, you can also Query arrays, using the same Query statements you would use against a local table or a database resource. If you're already familiar with SQL-style database query statements (such as create, read, update, delete) then you'll find these blocks very familiar.

Arrays respond to 4 queries, as shown below. Like queries against databases or tables, array queries return a custom datatype, depending on what you select.

Before we show some examples, let's stage some sample data (the same example as you'll see on the Array page ). Arrays can be of primitive or complex data types. For our examples we'll use complex. We'll use this custom type: 

Then, create a few variables of that type. 

Finally, declare your array, and add those variables to it to build a complex array.

The "add to array" block above isn't the only way to add an element to an array. You can also use the "Insert Into" query block.

In this example, we combine the "select first" (which ensures a max of 1 row is returned) and the "order by ascending" (which ensures this single row is the element with the lowest value). In this way, we get 1 element as our result: the one with the fewest moons. Some things to note here:

  1. Our "order by" is simply by the CountOfMoons column. If two or more rows all had the same smallest number in that column, it's not defined which one would be returned. To catch for edge cases like that, remove the "select first" (so you get a result set of 1 or more possible rows), and process those rows in a loop. Or, make your where clause more specific.
  2. Since we selected two columns (Name and CountOfMoons), we can use them in our result. If you want to use other fields as well, simply add them to the select section of the query block. You can also add the special "*" block, which will return all columns without you having to list each one explicitly.
  3. Any query can, in some cases, result in no matching rows. Before using the result of any query, remember to wrap it in an "if not is null" conditional block to be sure you actually have a result. In our example, that problem is very unlikely, but we still follow good programming habits and check for that result.

Here, any element in the array with a name of "Saturn" will be updated. This one statement can update 0, 1, or many rows.

Example of "Update" Query on an Array

Example of "Delete" Query on an Array

Here, any element in the array with a name of "Pluto" will be deleted. Again, this one statement can delete 0, 1, or many rows.

Was this article helpful?