Skip to content

Nesting Everything

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

Nesting everything 😄

We created nested arrays and nested containers, now we are going to nest everything(arrays with containers) a to create a more sophisticated system.

System representation in LDS:

classes = a:[
	a:[
		(:
			name=s:"Anita"
			average=f:16
			has_passed=b:1
			)
		(:
			name=s:"Carlos"
			average=f:14
			has_passed=b:1
			)
		(:
			name=s:"Zinha"
			average=f:8
			has_passed=b:0
			)
	]
]

Note:

  1. We have an array classes.
  2. Inside of classes[0] witch is the first class, we have some containers with students' data.

Implementation 🔨

First we create the classes and classes[0] arrays.

    // Creating classes
    database.declare("classes", lan::Array);
    
    // Creating classes[0]
    database.iterate("classes", 0, lan::Array);

Then we set the anchor to classes[0] to start adding students' data in the first class.

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

Now we create the first student

    // Creating the first student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[0] witch is a synonym to classes[0][0]
    database.set_anchor("@", 0);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Anita", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 16, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", true, lan::Bool);
    

Then the 2nd and 3rd

    // Second student
    
    // Set anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Creating the second student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[1] witch is a synonym to classes[0][1]
    database.set_anchor("@", 1);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Carlos", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 14, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", true, lan::Bool);
    
    // Third student
    
    // Set anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Creating the second student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[2] witch is a synonym to classes[0][2]
    database.set_anchor("@", 2);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Zinha", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 8, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", false, lan::Bool);

Printing Everything 📺

    for (int i = 0 ; i < 3 ; i++ ){
        // Set anchor to classes[0]
        database.set_anchor("classes", 0);
        // Set anchor to anchor[i] witch is a synonym to classes[0][i]
        database.set_anchor("@", i);
        
        std::cout << "Name: " << database.get< std::string >("@", "name", lan::String) << " - "
        << "Average: " << database.get< float >("@", "average", lan::Float) << " - "
        << "Passed: " << database.get< bool >("@", "has_passed", 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;
    
    // Creating classes
    database.declare("classes", lan::Array);
    
    // Creating classes[0]
    database.iterate("classes", 0, lan::Array);
    
    // First student
    
    // Set anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Creating the first student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[0] witch is a synonym to classes[0][0]
    database.set_anchor("@", 0);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Anita", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 16, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", true, lan::Bool);
    
    // Second student
    
    // Set anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Creating the second student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[1] witch is a synonym to classes[0][1]
    database.set_anchor("@", 1);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Carlos", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 14, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", true, lan::Bool);
    
    // Third student
    
    // Set anchor to classes[0]
    database.set_anchor("classes", 0);
    
    // Creating the second student
    database.iterate("@", 0, lan::Container);
    
    // Set anchor to anchor[2] witch is a synonym to classes[0][2]
    database.set_anchor("@", 2);
    
    // Adding student's data
    
    // Adding student.name
    database.set< std::string >("@", "name", "Zinha", lan::String);
    
    // Adding student.average
    database.set< float >("@", "average", 8, lan::Float);
    
    // Adding student.have_passed
    database.set< bool >("@", "has_passed", false, lan::Bool);

    // Printing everything
    for (int i = 0 ; i < 3 ; i++ ){
        // Set anchor to classes[0]
        database.set_anchor("classes", 0);
        // Set anchor to anchor[i] witch is a synonym to classes[0][i]
        database.set_anchor("@", i);
        
        std::cout << "Name: " << database.get< std::string >("@", "name", lan::String) << " - "
        << "Average: " << database.get< float >("@", "average", lan::Float) << " - "
        << "Passed: " << database.get< bool >("@", "has_passed", lan::Bool) << std::endl;
    }
    
    return 0;
}

Go back to Home.

Clone this wiki locally