How to Map result from db - list

So as we know rawQuery from Sqlite returning <List<Map<String, Object?>>>.
I need data in this form :
//example
Map<String, List> _dataItems = {
'1st ': ['Name01', 'SubData01'],
'2th': ['Name02', 'SubData02'],
'3th': ['Name03', 'SubData03'],
};
IM trying something like this:
List<Map<String, dynamic>> result = await dbService.getData();
var map = Map<String, dynamic>();
for (int i = 0; i < result.length; i++) {
map['placeCol'] = result[i]['Name01']
...
}
So basically return data from db placeCol to be key and few other String in the List, like in example data.
With this approach:
Map<String, List> _map = {};
for (int i = 0; i < result.length; i++) {
_map[result[i]['placeCol']] = [result[i]['NameCol']];
}
I get this:
{1st : [Name 01]}
{1st : [Name 02]}
{1st : [Name 03]}
{1st : [Name 04]}
{1st : [Name 05]}
{2st : [Name 01]}
{2st : [Name 02]}
{2st : [Name 03]}
Instad of this:
{1st : [Name 01, Name02,Name03,Name04,Name05],
2st : [Name 01, Name02,Name03]
}

Try Creating a map Directly from result map
So Something like this inside for loop
var map = result.elementAt(i)
And then you can use map to get data you want print it see how it looks to get a better idea of it's structure and use that to place it wherever you want

Related

Compared lists in google apps script

I am trying to compare 2 lists and find elements in list1 that are NOT present in list2.
list1: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161067, 01-0161068]
list2: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161066]
Elements in list1 but not in list2 are [01-0161067, 01-0161068]
I tried this code but it does not yield expected results:
missing = null,
i = list1.length;
while(i) {
missing = ( ~list1.indexOf(list2[--i]) ) ? missing : list1[i];
}
Any leads would be appreciated.
function findInOneNotInTwo() {
const list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
const list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
let in1notin2 = [];
list1.forEach(s=>{
if(!~list2.indexOf(s)) {
in1notin2.push(s);
}
});
Logger.log(in1notin2.join(', '));
}
Execution log
1:54:09 PM Notice Execution started
1:54:11 PM Info 01-0161067, 01-0161068
1:54:10 PM Notice Execution completed
var list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
var list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
var missing = list1.filter(x => !list2.includes(x));
console.log(missing); // [ '01-0161067', '01-0161068' ]

Check items from a List of ValueTuple and return results in another List of ValueTuple C#7

Please consider the following list of ValueTuple C#7
static void Main(string[] args)
{
List<(double prices, int matches)> myList = new List<(double, int)>();
myList.Add((100, 10));
myList.Add((100.50 , 12));
myList.Add((101 , 14));
myList.Add((101.50 , 16));
}
What would be a simple way to search for items meeting conditions for "prices" AND "matches" within the List and return the results in another List of ValueTuple.
For instance if I want to return another List of ValueTuples meeting "prices > 101 and matches > 6"
Can you post an example please?
It's easier if you give names to the items :
var myList = new List<(double d,int i)>
{
(100, 10),
(100.50 , 12),
(101 , 14),
(101.50 , 16)
};
var results = myList.Where(x => x.d>101 && x.i>6);
Without the names you'd have to write
var results = myList.Where(x => x.Item1>101 && x.Item2>6);
C# 7.3 added tuple equality but not comparison. You can now write :
var result = myList.Where(d=>d == (101,14));
and
var result = myList.Where(d=>d != (101,14));
But not
var result = myList.Where(d=>d > (101,14));

How to convert list of list to simple list by removing duplicate values using scala?

I have following list -
List(List(
List(((groupName,group1),(tagMember,["192.168.20.30","192.168.20.20","192.168.20.21"]))),
List(((groupName,group1),(tagMember,["192.168.20.30"]))),
List(((groupName,group1),(tagMember,["192.168.20.30","192.168.20.20"])))))
I want to convert it to -
List((groupName, group1),(tagMember,["192.168.20.30","192.168.20.20","192.168.20.21"]))
I tried to use .flatten but unable to form desired output.
How do I get above mentioned output using scala??
I had to make some changes to your input to make it valid.
Input List:
val ll = List(List(
List((("groupName","group1"),("tagMember", List("192.168.20.30","192.168.20.20","192.168.20.21")))),
List((("groupName","group1"),("tagMember",List("192.168.20.30")))),
List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20"))))
))
Code below works if the group, and tagMember are the same across all the elements in the list
def getUniqueIpsConstantGroupTagMember(inputList: List[List[List[((String, String), (String, List[String]))]]]) = {
// List[((String, String), (String, List[String]))]
val flattenedList = ll.flatten.flatten
if (flattenedList.size > 0) {
val group = flattenedList(0)._1
val tagMember = flattenedList(0)._2._1
val ips = flattenedList flatMap (_._2._2)
((group), (tagMember, ips.distinct))
}
else List()
}
println(getUniqueIpsConstantGroupTagMember(ll))
Output:
((groupName,group1),(tagMember,List(192.168.20.30, 192.168.20.20, 192.168.20.21)))
Now, let's assume you could have different groupNames.
Sample input:
val listWithVariableGroups = List(List(
List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20","192.168.20.21")))),
List((("groupName","group1"),("tagMember",List("192.168.20.30")))),
List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20")))),
List((("groupName","group2"),("tagMember",List("192.168.20.30","192.168.20.10"))))
))
The following code should work.
def getUniqueIpsForMultipleGroups(inputList: List[List[List[((String, String), (String, List[String]))]]]) = {
val flattenedList = inputList.flatten.flatten
// Map[(String, String),List[(String, List[String])]]
val groupedByGroupNameId = flattenedList.groupBy(p => p._1) map {
case (key, value) => (key, ("tagMember", extractUniqueTagIps(value)))
}
groupedByGroupNameId
}
def extractUniqueTagIps(list: List[((String, String), (String, List[String]))]) = {
val ips = list flatMap (_._2._2)
ips.distinct
}
getUniqueIpsForMultipleGroups(listWithVariableGroups).foreach(println)
Output:
((groupName,group1),(tagMember,List(192.168.20.30, 192.168.20.20, 192.168.20.21)))
((groupName,group2),(tagMember,List(192.168.20.30, 192.168.20.10)))

For loop to access dictionary

I have a NSDictionary of type String:AnyObject, and I want to have it be type String:String. How can I convert them with the same key to type string using a loop? I would think I could figure it out, but Xcode 6 sourcekit keeps crashing whenever I put in a for loop for the dictionary.
PS. I'm writing this in Swift, not Obj-C.
This way you can loop over the dictionary for objects:AnyObject:
let dict = ["A":1, "B":2, "C":3]
var string = ""
for object in dict.values {
string += "\(object)"
}
// string = "312"
If you want to loop over just the keys change to .keys as in the following:
for key in dict.keys {
string += key
}
// string = "CAB"
Finally to loop over both keys and values with a Tuple (key, object) :
let dict = ["A":1, "B":2, "C":3]
var string = ""
var sum = 0
for (key, object) in dict {
string += key
sum += object
}
// sum = 6
// string = "CAB"
Note: This works with Beta 3.

Returning list in ANTLR for type checking, language java

I am working on ANLTR to support type checking. I am in trouble at some point. I will try to explain it with an example grammar, suppose that I have the following:
#members {
private java.util.HashMap<String, String> mapping = new java.util.HashMap<String, String>();
}
var_dec
: type_specifiers d=dec_list? SEMICOLON
{
mapping.put($d.ids.get(0).toString(), $type_specifiers.type_name);
System.out.println("identext = " + $d.ids.get(0).toString() + " - " + $type_specifiers.type_name);
};
type_specifiers returns [String type_name]
: 'int' { $type_name = "int";}
| 'float' {$type_name = "float"; }
;
dec_list returns [List ids]
: ( a += ID brackets*) (COMMA ( a += ID brackets* ) )*
{$ids = $a;}
;
brackets : LBRACKET (ICONST | ID) RBRACKET;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
LBRACKET : '[';
RBRACKET : ']';
In rule dec_list, you will see that I am returning List with ids. However, in var_dec when I try to put the first element of the list (I am using only get(0) just to see the return value from dec_list rule, I can iterate it later, that's not my point) into mapping I get a whole string like
[#4,6:6='a',<17>,1:6]
for an input
int a, b;
What I am trying to do is to get text of each ID, in this case a and b in the list of index 0 and 1, respectively.
Does anyone have any idea?
The += operator creates a List of Tokens, not just the text these Tokens match. You'll need to initialize the List in the #init{...} block of the rule and add the inner-text of the tokens yourself.
Also, you don't need to do this:
type_specifiers returns [String type_name]
: 'int' { $type_name = "int";}
| ...
;
simply access type_specifiers's text attribute from the rule you use it in and remove the returns statement, like this:
var_dec
: t=type_specifiers ... {System.out.println($t.text);}
;
type_specifiers
: 'int'
| ...
;
Try something like this:
grammar T;
var_dec
: type dec_list? ';'
{
System.out.println("type = " + $type.text);
System.out.println("ids = " + $dec_list.ids);
}
;
type
: Int
| Float
;
dec_list returns [List ids]
#init{$ids = new ArrayList();}
: a=ID {$ids.add($a.text);} (',' b=ID {$ids.add($b.text);})*
;
Int : 'int';
Float : 'float';
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
Space : ' ' {skip();};
which will print the following to the console:
type = int
ids = [a, b, foo]
If you run the following class:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
TLexer lexer = new TLexer(new ANTLRStringStream("int a, b, foo;"));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.var_dec();
}
}