grouping - How to make pairs from Range? -
i have got range mysqltablesrange
. it's consist data like: aa_1 aa_3 aa_2 bb_2 bb_1 bb_3
i need create pairs like: aa_1 bb_1
aa_2 bb_2
aa_3 bb_3
std.algorithm
have method group doing similar thing, not know how write in code. did:
mysqltablesrange.each!(a => a.split("_")[1].array.group.writeln);
but it's wrong, because group
works array, not single element.
any ideas?
update: after testing - realised it's not 'group' want. chunkby. updated answer reflect that. https://dlang.org/phobos/std_algorithm_iteration.html#chunkby
you have tell chunkby how chunk data...
[1,2,3,4,5,6] .sort!((a,b) => a%2 > b%2) // separate odds n evens .chunkby!((a,b) => a%2 == b%2); // chunk them evens in 1 range, odds in another.
that create 2 groups. 1 odd numbers, 1 evens.
in case looks you'd group them on text comes after '_' in each element.
"aa_1 aa_2 aa_3 bb_1 bb_2 bb_3 cc_1" .split(" ") .sort!((a,b) => a[$-1].to!int < b[$-1].to!int) // sort _1's together, _2s together. etc .chunkby!((a,b) => a[$-1] == b[$-1]) // chunk them they're in they're own range .each!writeln; // print each range $ rdmd test.d ["aa_1", "bb_1", "cc_1"] ["aa_2", "bb_2"] ["aa_3", "bb_3"]
ideally you'd index of _ , compare after that...
Comments
Post a Comment