I have a list of offset vars and i need to put it in another list so
I can call it like
List<list<offset>> coords
coords[0][i] = points[i]
But it didnt work.
Any suggestion??
You need to initialize your lists before:
final size = points.size;
var coords = List<List<offset>>.generate(size,(i) => List<offset>.generate(size, (j) => 0));
coords[0][i]=points[i];
Related
I want to store the values by selecting the keys of a table into another table, for example:
polyline = {color="blue", thickness=2, npoints=4}
stuff = {"polyline.color":[polyline.thickness]}
print(stuff)
Should produce:
blue 2
However, I get the following error:
input:3: '}' expected near ':'
local polyline = {color="blue", thickness=2, npoints=4}
local stuff = {polyline.color, polyline.thickness}
print(table.unpack(stuff))
I believe, You're mixing in some Python syntax. Do you notice using two different (wrong) ways of accessing the values?
I guess, this is what You've meant with your snippet of Lua code:
polyline = {color = "blue", thickness = 2, npoints = 4}
stuff = {[polyline.color] = polyline.thickness}
for key, val in pairs(stuff) do
print(key, val)
end
Sometimes I see this
List list = [];
Then list..add(color)
What's the difference between using 1 dot(.) and 2 dot(..)?
.. is known as cascade notation. It allows you to not repeat the same target if you want to call several methods on the same object.
List list = [];
list.add(color1);
list.add(color2);
list.add(color3);
list.add(color4);
// with cascade
List list = [];
list
..add(color1)
..add(color2)
..add(color3)
..add(color4);
It's the cascade operator of Dart
var l1 = new List<int>()..add(0)..addAll([1, 2, 3]);
results in l1 being a list [0, 1, 2, 3]
var l1 = new List<int>().add(0).addAll([1, 2, 3]);
results in an error, because .add(0) returns void
.. (in the former example) refers to new List(),
while . (in the later) refers to the return value of the previous part of the expression.
.. was introduced to avoid the need to return this in all kinds of methods like add() to be able to use an API in a fluent way.
.. provides this out of the box for all classes.
Cascades (..) allow you to make a sequence of operations on the same object. read doc for details
querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
The previous example is equivalent to:
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
Double dots(..) also know as cascade operator
It allows you to not repeat the same target if you want to call several methods on the same object.
e.g without double dots
var paint = Paint();
paint.color = Colors.black;
paint.strokeCap = StrokeCap.round;
paint.strokeWidth = 5.0;
But after using “..”, the above code will be written like this:
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
Triple dots(…) i.e. Spread Operator
“… ”also known as spread operator which provide a concise way to insert multiple values into a collection.
You can use this to insert all the elements of a list into another list:
Normally we use .add() or .addAll() to add data to the list like:
var list = [1, 2, 3];
var list2=[];
list2.addAll(list);
After using “…” we will write code like this:
var list = [1, 2, 3];
var list2 = [0, ...list];
.. Is known as the cascading operator in dart.
It allows you to use more than one subsequence operation:
Examples:
banerad..load()..show().
List coursename;
coursename..add("java")..add("flutter" )..add("dart");
Here is another example
Ok so this is my scenario:
rascal>map[int, list[int]] g = ();
rascal>g += (1:[2]);
This will result in:
rascal>g[1];
list[int]: [2]
So far so good, but now I wanted to do this, but it didn't work:
rascal>g[1] += 3;
|stdin:///|(2,1,<1,2>,<1,3>): insert into collection not supported on value and int
So I can't directly use the value from g[1] and will have to use a temporary variable like this:
rascal>lst = g[1];
rascal>lst += 3;
rascal>g[1] = lst;
map[int, list[int]]: (1:[2,3])
But doing this everytime I want to extent my list is a drag!
Am I doing something wrong or would this be an awesome feature?
Richard
Good question! + on lists is concatenation not insert, so you could type the following to get the desired effect:
g[1] += [2];
I'm trying to see if it's possible to get all the objects with the same name. I'm using the following code to load a bunch of circles on the screen. They all have the same
local myCircle = display.newCircle(30+(yCount*20), 220+(yCount*10), 8)
myCircle.name = "peg"
I would imagine there is a way to do this but I'm not sure where to even look for such a thing.
Thanks
First of all you need a circle array
array = {}
Then when you create a single circle, you should add that circle to the array
array[#array+1] = myCircle
Now here is the find function by spesific name
local function findByName( name )
local resultArray = {}
for i=1, #array do
if array[i].name == name then
resultArray[#resultArray+1] = array[i]
end
end
return resultArray
end
So, at the end, when you call
local tempArray = findByName( "peg" )
you'll get circles named "peg" in tempArray
I have created a bunch of movie clips which all have similar names and then after some other event I have built up a string like:
var clipName = "barLeft42"
which is held inside another movie clip called 'thing'.
I have been able to get hold of a reference using:
var movieClip = Eval( "_root.thing." + clipName )
But that feels bad - is there a better way?
Movie clips are collections in actionscript (like most and similar to javascript, everything is basically key-value pairs). You can index into the collection using square brackets and a string for the key name like:
_root.thing[ "barLeft42" ]
That should do the trick for you...
The better way, which avoids using the deprecated eval, is to index with square brackets:
var movieClip = _root.thing[ "barLeft42" ]
But the best way is to keep references to the clips you make, and access them by reference, rather than by name:
var movieClipArray = new Array();
for (var i=0; i<45; i++) {
var mc = _root.thing.createEmptyMovieClip( "barLeft"+i, i );
// ...
movieClipArray.push( mc );
}
// ...
var movieClip = movieClipArray[ 42 ];
You can use brackets and include variables within them... so if you wanted to loop through them all you can do this:
for (var i=0; i<99; i++) {
var clipName = _root.thing["barLeft"+i];
}