Is there a library that would format structure tables?
something like:
struct a {
char *s;
int c;
int b;
} sample[] = {
{"this is a test", 1, 4},
{"this",3,56544}
};
So the reformatting would be :
} sample[] = {
{"this is a test", 1, 4},
{"this" , 3, 56544}
};
Set a region that includes all the values in your sample[] array, and then C-u M-x align that region.
Related
Dart Can't change map value from initial map
I have this code :
void main() {
Map tabtest = {
'1' : {
'd' : 'one'
}
};
Map tabtest1 = {};
tabtest.forEach((cle1, val1) {
val1['d']='two';
tabtest1['cle1']=val1;
});
Map tabtest2={};
tabtest.forEach((cle2, val2) {
val2['d']='three';
tabtest2['cle2']=val2;
});
print(tabtest);
print(tabtest1);
print(tabtest2);
}
This code display :
{1: {d: three}}
{1: {d: three}}
{1: {d: three}}
But I want different value from tabtest1 and tabtest2 like :
tabtest1 = {1: {d: two}}
tabtest2 = {1: {d: three}}
Can help me?
Thanks.
There were a couple of problems:
you were actually using the same sub-map {'d': 'one'} in all three versions, instead of creating three seperate maps. The easiest way to copy a Map is to use Map secondMap = Map.from(firstMap). Beware though - this will only copy the first 'level' of the map. See this question for more information
you were using the string 'cle1' instead of it's value, so tabtest1['cle1'] should have been tabtest1[cle1]
void main() {
Map tabtest = {
'1' : {
'd' : 'one'
}
};
Map tabtest1 = {};
// For each map entry in tabtest
tabtest.forEach((cle, val) {
// First COPY the sub-map. If we don't make a copy, we end up
// changing the sub-map in both tabtest and tabtest2
Map val1 = Map.from(val);
// Then we can update the value in our new sub-map:
val1['d']='two';
// And set this as the key '1' in tabtest1
tabtest1[cle]=val1;
});
Map tabtest2={};
tabtest.forEach((cle, val) {
Map val2 = Map.from(val);
val2['d']='three';
tabtest2[cle]=val2;
});
print(tabtest);
print(tabtest1);
print(tabtest2);
}
{1: {d: one}}
{1: {d: two}}
{1: {d: three}}
map1 = { "a": 10, "b": 6 },
map2 = { "a": 10, "b": 6, "c": 7, "d": 8 };
Flutter:How to merge two objects and sum the values of the same key?
Do forEach on the longest map and check if the small map contains the key if it does then update the value with the sum or add the new.
map2.forEach((key, value) {
if (map1.containsKey(key)) {
map1[key] = value + map1[key]!;
} else {
map1[key] = map2[key]!;
}
});
map1 will be the final result.
So, if you want to combine/merge the two maps use this code this answer:
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = { // here simple adding element to map
...firstMap,
...secondMap,
};
but if you want to make sum and merge use this :
map2.forEach((k, v) {
if (map1.containsKey(k)) { // check if the map has more then 2 values as the 1st one
map1[k] = v + map1[k]!; // if yes so make the some
} else {
map1[k] = map2[k]!; // if no then add the values to map
}
});
as MEET Prajapati asnwer.
So I have a List of Maps of two dynamic variables.
[{Title: product 1, Item Score: 87.3, Characters: 72, Image Count: 6},
{Title: product 2, Item Score: 85.1, Characters: 56, Image Count: 2}]
How would I add up all of them item scores? There are more items than this in the full file.
I would look at fold in the standard library https://api.dart.dev/stable/1.10.1/dart-core/List/fold.html.
With fold, you iterate over the list of maps, and the first argument is your starting value. In the case of sum, this would be 0. Your inner function in the fold takes 2 arguments, the current accumlation(sum), and the item in your list that is currently being iterated over.
E.g.:
const itemScores = [{'itemScore': 1}, {'itemScore': 2}, {'itemScore': 44}];
var sum = itemScores.fold(0, (i, el){
return i + el['itemScore'];
});
Where i is the current value of the accumulator, and el is the current item in the list being iterated over.
Assuming that, you need to store the value in some variable, you can simply use List.forEach((element){ })
Advantage, you can iterate over as many data as you can, and then store the value
void main(){
var data = [{"Title": "product 1", "Item Score": 87.3, "Characters": 72, "Image Count": 6}, {"Title": "product 2", "Item Score": 85.1, "Characters": 56, "Image Count": 2}];
double totalScores = 0.0;
// looping over data array
data.forEach((item){
//getting the key direectly from the name of the key
totalScores += item["Item Score"];
});
print(totalScores); // OUTPUT ==> 172.39999999999998
}
List arr = [[1,2,3],[4,5,7],[8,9,10],[14,15,19]];
int stairsIn203(List<List<int>> arr) {
// your code here
int _a = 0 ;
List _b = [];
for(List i in arr){
_b += i;
}
_a = _b.reduce((value, element) => value + element) * 20;
return _a;
}
Or
int stairsIn20(List<List<int>> arr) => arr.expand((e) => e).reduce((v, e) => v + e) * 20;
var sum = [{Title: product 1, Item Score: 87.3, Characters: 72, Image
Count: 6}, {Title: product 2, Item Score: 85.1, Characters: 56, Image
Count: 2}]].reduce((item1, item2) => item1["Item Score"] +item2["Item
Score"]);
As part of my studies, we recently started learning MongoDB, and so I am very young in the field.
In one of the tasks we have been assigned to, MapReduce is used, but I can not do it properly.
Giving a collection of students of the following form:
{
_id: ObjectId("4ffmdjd8cfd99k03"),
tz: "11111",
FirstName: "Anton",
LastName: "Gill",
Department: "Computer Science"
Year: 1
Courses: [ { name: "Linear Algebra 1", grade: 70}, {name: "OS", grade: 88}]
}
The task is to write a mapReduce function, so that it returns a list of the names of the students in each department and each year, all whose grades are greater than 90. Example of normal output:
[{Department: "Computer Science", Year: 1, students: ["Anton", "John"]},
{Department: "Physics", Year: 2, students: ["Dean", "David"]}]
Can I write more than one map function? That is, the mapReduce structure will look like this:
db.students.mapReduce(
map1,
map2,
map3,
..
reduce,
..
I try unsuccessfully to create the desired structure.
This is the best I've been able to get to, and I'm still not sure how to write the reduce function.
var map = function(){
var value = 0, n = this.Courses.length;
for(var i = 0; i < n; i++){
value += this.Courses[i].grade;
}
var avgGrade = value/n;
if(avgGrade > 90){
var key = {
tz:this.tz,
Department:this.Department,
Year:this.Year,
};
value = {students:[this.FirstName]};
}
emit(key, value);
};
var reduce = function(keysObj, valuesObj){
return 1000; //Ignore this function, I've no idea how to deal with it.
};
db.students.mapReduce(
map,
reduce,
{
out: "output",
}
)
I would highly appreciate any assistance :)
I have doc collection which have object type field named price (i.e., see below), I just want to update/insert that field by adding new key value pairs to it.
suppose i have this as collection (in db):
[
{
_id: 1,
price: {
amazon: 102.1,
apple: 500
}
},
....
....
];
Now I want to write an query which either update price's or inserts if not exist in price.
let's suppose these as input data to update/insert with:
var key1 = 'ebay', value1 = 300; // will insert
var key2 = 'amazon', value2 = 100; // will update
assume doc having _id: 1 for now.
Something like $addToSet operator?, Though $addToSet only works for array & i want to work within object).
expected output:
[
{
_id: 1,
price: {
amazon: 100, // updated
apple: 500,
ebay: 300 // inserted
}
},
....
....
];
How can i do/achieve this?
Thanks.
You could construct the update document dynamically to use the dot notation and the $set operator to do the update correctly. Using your example above, you'd want to run the following update operation:
db.collection.update(
{ "_id": 1 },
{
"$set": { "price.ebay": 300, "price.amazon": 100 }
}
)
So, given the data input, you would want to construct an update document like { "price.ebay": 300, "price.amazon": 100 }
With the inputs as you have described
var key1 = 'ebay', value1 = 300; // will insert
var key2 = 'amazon', value2 = 100; // will update
Construct the update object:
var query = { "_id": 1 },
update = {};
update["price."+key1] = value1;
update["price."+key2] = value2;
db.collection.update(query, {"$set": update});