Skip to content

Nested Arrays

René Descartes Muala edited this page May 1, 2021 · 1 revision

Nested Arrays

[ [1️⃣ , 2️⃣, 3️⃣ ] ,[ 🅰️, 🅱️] , 🌄 , [ 🚗, 🚓, 🛻, [ 🚒 ,🚚 ,🚛 ] ] ]

Nested Arrays are just arrays inside of another arrays, they are available in c++ as matrices (eg: char names[20][30]) but landb allows us to go more deeper and nest how much arrays we want.

  • Example in LDS:
numbers = a : [
	a:[i:0 s:"zero"]
	a:[i:1 s:"one"   s:"I"]
	a:[i:2 s:"two"   s:"II"]
	a:[i:3 s:"three" s:"III"]
	a:[i:4 s:"four"  s:"IV"]
	...
]

Note: There isn't a maximum or minimum length, and if we want, we could create another arrays inside of the arrays in numbers[].

The Anchor aka "@" ⚓️

Lets suppose that we have an array:

classes = a : [
	a:[
		a:[s:"Anita"	f:18	b:1]
		a:[s:"Carlos"	f:12	b:1]
		a:[s:"Zinha"	f:8	b:0]
	]
	a:[
		a:[s:"Celia"	f:16	b:1]
		a:[s:"Nilton"	f:3	b:0]
		a:[s:"Mario"	f:13	b:1]
	]
]

It's a little bit weird, but lets take a look:

  • There are two classes (arrays) that store data (another arrays) in the following structure [student_name, student_average, student_has_passed]
  • Each class is a nested array, that way, we can access the name of the first student in the first class by classes[0][0][0].

So, instead of using ugly indexes like city[2][4][0][3][4] every time that we need to get access a certain data. Landb allows us to use the anchor, and the process is very simple!

1º. We set_anchor() position.

[ 0 - 1 - 2 - 3 - 4 - 5 - ⚓️ - 7 - 8 ]

2º. Then, every time that we need to access that data, we use the anchor address ("@") and landb doesn't need to check is that is a valid index or array, increasing performance.

[ 0 - 1 - 2 - 3 - 4 - 5 - ⛵️ - 7 - 8 ]

The set_anchor() method in arrays 🙃

    // Usage
    database.set_anchor("array_name", array_index);
    // Example - Setting the anchor to classes[0]
    database.set_anchor("classes", 0);

Declaring a Nested Array [ 🚗, [ 🚛 ] ]

(Example program to create the first class of our classes example above)

First we declare an array.

    // Declaring an array
    database.declare("classes", lan::Array);

Appending data in a Nested Array 0️⃣ > 1️⃣ > 2️⃣ > 3️⃣ ...

Then we append that array data, but instead of giving a variable, we give an lan::Array, and set the data to 0.

    // Adding the first class
    database.iterate("classes", 0, lan::Array);

Set the anchor to classes[0]

    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);

After that, we create another array to store the first student's data

    // Adding an array in classes[0] to store data of the first student 
    // Using the anchor to access classes[0]
    database.iterate("@", 0, lan::Array);

Then we set the anchor to point to the first student's data

    // Setting the anchor to anchor[0] with is a synonym of classes[0][0]
    database.set_anchor("@", 0);

Now we need to add the first student's data, with is being pointed to by anchor

    // Adding student name to classes[0][0][0]
    database.iterate< std::string >("@", "Anita", lan::String);
    
    // Adding student average to classes[0][0][1]
    database.iterate< float >("@", 18, lan::Float);
    
    // Adding student has_passed to classes[0][0][2]
    database.iterate< bool >("@", true, lan::Bool);

Adding the 2nd and 3rd students' data

    // Second student
    
    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Adding an array in classes[0] to store data of the second student
    database.iterate("@", 0, lan::Array);
    
    // Setting the anchor to anchor[1] with is a synonym of classes[0][1]
    database.set_anchor("@", 1);
    
    // Adding student name to classes[0][1][0]
    database.iterate< std::string >("@", "Carlos", lan::String);
    
    // Adding student average to classes[0][1][1]
    database.iterate< float >("@", 12, lan::Float);
    
    // Adding student has_passed to classes[0][1][2]
    database.iterate< bool >("@", true, lan::Bool);
    
    // Third student
    
    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Adding an array in classes[0] to store data of the third student
    database.iterate("@", 0, lan::Array);
    
    // Setting the anchor to anchor[2] with is a synonym of classes[0][2]
    database.set_anchor("@", 2);
    
    // Adding student name to classes[0][2][0]
    database.iterate< std::string >("@", "Zinha", lan::String);
    
    // Adding student average to classes[0][2][1]
    database.iterate< float >("@", 8, lan::Float);
    
    // Adding student has_passed to classes[0][2][2]
    database.iterate< bool >("@", false, lan::Bool);

Printing the Nested Array data 📺

    for (int i = 0 ; i < 3 ; i ++){
        // Setting the anchor to classes[0][i]
        database.set_anchor("classes", 0);
        database.set_anchor("@", i);

        // Printing everything
        std::cout << "Name: " << database.get< std::string >("@", 0, lan::String) << " - "
        << "Average: " << database.get< float >("@", 1, lan::Float) << " - "
        << "Passed: " << database.get< bool >("@", 2, lan::Bool) << std::endl;
    }
    
    return 0;
}

Here is the entire code {✨}

#include <iostream>
#include "landb.hpp"

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
        
    // Declaring an array
    database.declare("classes", lan::Array);
    
    // Adding the first class
    database.iterate("classes", 0, lan::Array);
    
    
    // First student
    
    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Adding an array in classes[0] to store data of the first student
    // Using the anchor to access classes[0]
    database.iterate("@", 0, lan::Array);
    
    // Setting the anchor to anchor[0] with is a synonym of classes[0][0]
    database.set_anchor("@", 0);
    
    // Adding student name to classes[0][0][0]
    database.iterate< std::string >("@", "Anita", lan::String);
    
    // Adding student average to classes[0][0][1]
    database.iterate< float >("@", 18, lan::Float);
    
    // Adding student has_passed to classes[0][0][2]
    database.iterate< bool >("@", true, lan::Bool);
    
    // Second student
    
    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Adding an array in classes[0] to store data of the second student
    database.iterate("@", 0, lan::Array);
    
    // Setting the anchor to anchor[1] with is a synonym of classes[0][1]
    database.set_anchor("@", 1);
    
    // Adding student name to classes[0][1][0]
    database.iterate< std::string >("@", "Carlos", lan::String);
    
    // Adding student average to classes[0][1][1]
    database.iterate< float >("@", 12, lan::Float);
    
    // Adding student has_passed to classes[0][1][2]
    database.iterate< bool >("@", true, lan::Bool);
    
    // Third student
    
    // Setting the anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Adding an array in classes[0] to store data of the third student
    database.iterate("@", 0, lan::Array);
    
    // Setting the anchor to anchor[2] with is a synonym of classes[0][2]
    database.set_anchor("@", 2);
    
    // Adding student name to classes[0][2][0]
    database.iterate< std::string >("@", "Zinha", lan::String);
    
    // Adding student average to classes[0][2][1]
    database.iterate< float >("@", 8, lan::Float);
    
    // Adding student has_passed to classes[0][2][2]
    database.iterate< bool >("@", false, lan::Bool);
    
    for (int i = 0 ; i < 3 ; i ++){
        // Setting the anchor to classes[0][i]
        database.set_anchor("classes", 0);
        database.set_anchor("@", i);

        // Printing everything
        std::cout << "Name: " << database.get< std::string >("@", 0, lan::String) << " - "
        << "Average: " << database.get< float >("@", 1, lan::Float) << " - "
        << "Passed: " << database.get< bool >("@", 2, lan::Bool) << std::endl;
    }
    
    return 0;
}

Go back to Home.