Data Structures

Maps (Objects)

Maps in BanglaCode are collections of key-value pairs, similar to objects in JavaScript or dictionaries in Python. Keys are strings and values can be any type.

Creating Maps

1// Empty map
2dhoro empty = {};
3
4// Map with properties
5dhoro person = {
6 naam: "Rahim Khan",
7 boyosh: 25,
8 city: "Dhaka"
9};
10
11// Properties can be any value type
12dhoro data = {
13 string: "hello",
14 number: 42,
15 boolean: sotti,
16 array: [1, 2, 3],
17 nested: {
18 x: 10,
19 y: 20
20 }
21};

Accessing Properties

1dhoro person = {
2 naam: "Rahim",
3 boyosh: 25,
4 city: "Dhaka"
5};
6
7// Dot notation
8dekho(person.naam); // "Rahim"
9dekho(person.boyosh); // 25
10
11// Bracket notation
12dekho(person["city"]); // "Dhaka"
13
14// Dynamic property access
15dhoro key = "naam";
16dekho(person[key]); // "Rahim"
17
18// Nested access
19dhoro company = {
20 name: "TechCorp",
21 address: {
22 street: "123 Main St",
23 city: "Kolkata",
24 zip: "700001"
25 }
26};
27
28dekho(company.address.city); // "Kolkata"
29dekho(company["address"]["zip"]); // "700001"

Modifying Maps

1dhoro user = {
2 naam: "Rahim",
3 email: "rahim@example.com"
4};
5
6// Modify existing property
7user.naam = "Rahim Khan";
8dekho(user.naam); // "Rahim Khan"
9
10// Add new property
11user.phone = "01234567890";
12user["address"] = "Dhaka, Bangladesh";
13
14dekho(user);
15// {naam: "Rahim Khan", email: "rahim@example.com", phone: "01234567890", address: "Dhaka, Bangladesh"}

Map Built-in Functions

chabi - Get All Keys

1dhoro person = {
2 naam: "Karim",
3 boyosh: 30,
4 job: "Engineer",
5 city: "Chittagong"
6};
7
8dhoro keys = chabi(person);
9dekho(keys); // ["naam", "boyosh", "job", "city"]
10
11// Iterate over keys
12ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
13 dhoro key = keys[i];
14 dekho(key + ":", person[key]);
15}

Checking for Properties

1dhoro config = {
2 host: "localhost",
3 port: 8080
4};
5
6// Check if property exists
7dhoro keys = chabi(config);
8
9jodi (ache(keys, "host")) {
10 dekho("Host is configured:", config.host);
11}
12
13jodi (na ache(keys, "timeout")) {
14 dekho("Timeout not configured, using default");
15 config.timeout = 30;
16}
17
18// Check for null/undefined value
19jodi (config.database == khali) {
20 dekho("Database not configured");
21}

Nested Maps

1dhoro organization = {
2 name: "Tech Company",
3 departments: {
4 engineering: {
5 head: "Alice",
6 employees: 50
7 },
8 marketing: {
9 head: "Bob",
10 employees: 20
11 },
12 hr: {
13 head: "Carol",
14 employees: 10
15 }
16 },
17 locations: ["Dhaka", "Kolkata", "Mumbai"]
18};
19
20// Deep access
21dekho(organization.departments.engineering.head); // "Alice"
22dekho(organization.departments.marketing.employees); // 20
23
24// Modify nested
25organization.departments.engineering.employees = 55;
26
27// Add new nested property
28organization.departments.sales = {
29 head: "David",
30 employees: 15
31};

Maps with Methods

1dhoro calculator = {
2 value: 0,
3
4 add: kaj(n) {
5 ei.value = ei.value + n;
6 ferao ei;
7 },
8
9 subtract: kaj(n) {
10 ei.value = ei.value - n;
11 ferao ei;
12 },
13
14 multiply: kaj(n) {
15 ei.value = ei.value * n;
16 ferao ei;
17 },
18
19 getResult: kaj() {
20 ferao ei.value;
21 },
22
23 reset: kaj() {
24 ei.value = 0;
25 ferao ei;
26 }
27};
28
29// Note: 'ei' in map methods refers to the map itself
30calculator.add(10).multiply(2).subtract(5);
31dekho(calculator.getResult()); // 15

Iterating Over Maps

1dhoro scores = {
2 Rahim: 85,
3 Karim: 92,
4 Jamil: 78,
5 Nasir: 88
6};
7
8dhoro keys = chabi(scores);
9
10// Print all entries
11ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
12 dhoro name = keys[i];
13 dekho(name + ": " + lipi(scores[name]));
14}
15
16// Calculate average
17dhoro total = 0;
18ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
19 total = total + scores[keys[i]];
20}
21dekho("Average:", total / dorghyo(keys));
22
23// Find highest score
24dhoro highest = 0;
25dhoro topStudent = "";
26ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
27 jodi (scores[keys[i]] > highest) {
28 highest = scores[keys[i]];
29 topStudent = keys[i];
30 }
31}
32dekho("Top student:", topStudent, "with", highest);

Practical Examples

Configuration Object

1dhoro config = {
2 app: {
3 name: "MyApp",
4 version: "1.0.0",
5 debug: mittha
6 },
7 database: {
8 host: "localhost",
9 port: 5432,
10 name: "mydb"
11 },
12 features: {
13 darkMode: sotti,
14 notifications: sotti,
15 analytics: mittha
16 }
17};
18
19// Access configuration
20jodi (config.features.darkMode) {
21 dekho("Dark mode enabled");
22}
23
24// Build connection string
25dhoro db = config.database;
26dhoro connStr = db.host + ":" + lipi(db.port) + "/" + db.name;
27dekho("Connection:", connStr);

Word Counter

1dhoro text = "apple banana apple cherry banana apple";
2dhoro words = bhag(text, " ");
3dhoro counts = {};
4
5ghuriye (dhoro i = 0; i < dorghyo(words); i = i + 1) {
6 dhoro word = words[i];
7 jodi (counts[word] == khali) {
8 counts[word] = 0;
9 }
10 counts[word] = counts[word] + 1;
11}
12
13dekho(counts);
14// {apple: 3, banana: 2, cherry: 1}
15
16// Find most common word
17dhoro keys = chabi(counts);
18dhoro maxCount = 0;
19dhoro maxWord = "";
20
21ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
22 jodi (counts[keys[i]] > maxCount) {
23 maxCount = counts[keys[i]];
24 maxWord = keys[i];
25 }
26}
27
28dekho("Most common:", maxWord, "(", maxCount, "times)");

Data Transformation

1// Array of maps
2dhoro users = [
3 {id: 1, naam: "Rahim", role: "admin"},
4 {id: 2, naam: "Karim", role: "user"},
5 {id: 3, naam: "Jamil", role: "user"},
6 {id: 4, naam: "Nasir", role: "moderator"}
7];
8
9// Group by role
10dhoro byRole = {};
11
12ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) {
13 dhoro user = users[i];
14 dhoro role = user.role;
15
16 jodi (byRole[role] == khali) {
17 byRole[role] = [];
18 }
19 dhokao(byRole[role], user.naam);
20}
21
22dekho(byRole);
23// {admin: ["Rahim"], user: ["Karim", "Jamil"], moderator: ["Nasir"]}
24
25// Create lookup by ID
26dhoro byId = {};
27ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) {
28 dhoro user = users[i];
29 byId[lipi(user.id)] = user;
30}
31
32// Quick lookup
33dekho(byId["2"].naam); // "Karim"

JSON-like Data

1dhoro apiResponse = {
2 status: "success",
3 code: 200,
4 data: {
5 users: [
6 {id: 1, naam: "User 1", active: sotti},
7 {id: 2, naam: "User 2", active: mittha}
8 ],
9 total: 2,
10 page: 1
11 },
12 meta: {
13 timestamp: somoy(),
14 server: "api-1"
15 }
16};
17
18// Process response
19jodi (apiResponse.status == "success") {
20 dhoro users = apiResponse.data.users;
21
22 ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) {
23 dhoro user = users[i];
24 jodi (user.active) {
25 dekho("Active user:", user.naam);
26 }
27 }
28}
29
30// Convert to JSON string
31dhoro jsonStr = json_banao(apiResponse);
32dekho(jsonStr);