Shorten array in actionscript? - actionscript

How can I make this code shorter, is there any way? I feel there's too much repeating..
var bokstaver1:Array = new Array("a", "b", "c");
var bokstaver2:Array = ["d","e","f"];
var bokstaver:Array = New Array();
bokstaver[0] = "b";
bokstaver[1] = "i";
bokstaver[2] = "l";
bokstaver[3] = "l";
bokstaver[4] = "e";
I'm all new here so if this is not a way to ask a question on here please don't hasten insults.

You can do it in this easy way:
var bokstaver:Array = "bille".split("");
trace(bokstaver); // outputs: b,i,l,l,e

Related

i am new in action script. trying to split a sentence into words and show them in sprite.and also need which sprite i clicked

I am trying to split a sentence in to words and show them into sprite()
var sentence:String = "My name is Subhadip.";
var txt:Array = new Array();
var splittedSentence:Array = sentence.split(" ");
var myArraySprites:Array = new Array();
var myTextImage:Array = new Array();
var div = 40;
for (var i:int = 0; i < splittedSentence.length; i++)
{
var v = 300 - (div * i);
//...
txt[i] = new TextField();
txt[i].autoSize = TextFieldAutoSize.CENTER;
txt[i].text = splittedSentence[i];
var format1:TextFormat = new TextFormat();
format1.size = 24;
txt[i].setTextFormat(format1);
trace(txt[i]);
myArraySprites[i] = new Sprite();
myArraySprites[i].graphics.lineStyle(1, 0x000000, 1);
myArraySprites[i].buttonMode = true;
myArraySprites[i].graphics.beginFill( 0xfffffff );
myArraySprites[i].graphics.drawRect(50, v, 150, div);
myTextImage[i] = new BitmapData(100,v,true,0xffffff);
myTextImage[i].draw(txt[i]);
trace(myTextImage[i]);
//myArraySprites[i].graphics.beginBitmapFill(myTextImage[i]);
//myArraySprites[i].graphics.endFill();
myArraySprites[i].addChild(new Bitmap(myTextImage[i]));
addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);
}
function removeThis(e:MouseEvent):void
{
var clickTarget:int = myArraySprites.indexOf(e.currentTarget);
trace("Clicked sprite (id): " + clickTarget);
}
[enter image description here][1]
trying to split a sentence into words and show them in sprite.and also need which sprite i clicked
[1]: https://i.stack.imgur.com/RzHUQ.png
The problem is that you're dealing with screen positions in two different places:
myArraySprites[i].graphics.drawRect(50, v, 150, div);
and
myTextImage[i] = new BitmapData(100,v,true,0xffffff);
Think of it in a more object-oriented way. Each Sprite instance you want to be clickable is a single object. As such it also holds the main properties e.g. the on-screen position determined by it's x and y properties.
Each of those sprites have equal width & height, so make this a global property.
var wid:Number = 150;
var hei:Number = 40;
If we look at your .png, we can see that you want to have a single word centered inside such a sprite. To make things easier, we can use the .align property of the TextFormat class, set it to "center" and make each TextField 150 x 40 by using the wid and hei properties.
Finally each sprite's vertical screen position is shifted up by hei == 40 pixels. So let's determine a start position:
var yPos:Number = 300;
assign it to a sprite instance:
myArraySprites[i].x=50;
myArraySprites[i].y=yPos;
and decrement it by hei inside the for-loop.
Everything put together:
var yPos:Number = 300;
var wid:Number = 150;
var hei:Number = 40;
var sentence:String = "My name is Subhadip.";
var txt:TextField = new TextField();
txt.width = wid;
txt.height = hei;
var format1:TextFormat = new TextFormat();
format1.size = 24;
format1.align = "center";
var splittedSentence:Array = sentence.split(" ");
var myTextImage:Array = new Array();
for (var i:int = 0; i<splittedSentence.length; i++)
{
txt.text = splittedSentence[i];
txt.setTextFormat(format1);
myArraySprites[i] = new Sprite();
myArraySprites[i].x = 50;
myArraySprites[i].y = yPos;
myArraySprites[i].graphics.lineStyle(1, 0x000000, 1);
myArraySprites[i].buttonMode = true;
myArraySprites[i].graphics.beginFill(0xfffffff);
myArraySprites[i].graphics.drawRect(0, 0, wid, hei);
myTextImage[i] = new BitmapData(wid, hei, true, 0xffffff);
myTextImage[i].draw(txt);
myArraySprites[i].addChild(new Bitmap(myTextImage[i]));
addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);
yPos -= hei;
}

Combine/merge multiple maps into 1 map

How can I combine/merge 2 or more maps in dart into 1 map?
for example I have something like:
var firstMap = {"1":"2"};
var secondMap = {"1":"2"};
var thirdMap = {"1":"2"};
I want:
var finalMap = {"1":"2", "1":"2", "1":"2"};
you can use addAll method of Map object
var firstMap = {"1":"2"};
var secondMap = {"2":"3"};
var thirdMap = {};
thirdMap.addAll(firstMap);
thirdMap.addAll(secondMap);
print(thirdMap);
Or
var thirdMap = {}..addAll(firstMap)..addAll(secondMap);
Update
Since dart sdk 2.3
You can use spread operator ...
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = {
...firstMap,
...secondMap,
};
alternate syntax using Map.addAll, Iterable.reduce and cascading operator, for combining a lot of maps:
var combinedMap = mapList.reduce( (map1, map2) => map1..addAll(map2) );
live dartpad example
https://dartpad.dartlang.org/9cd116d07d2f45a9b890b4b1186dcc5e
Another option is using CombinedMapView from package:collection:
new CombinedMapView([firstMap, secondMap])
It doesn't create a merged map, but creates a Map that is a view of both.
I came up with a "single line" solution for an Iterable of Maps:
var finalMap = Map.fromEntries(mapList.expand((map) => map.entries));
var firstMap = {"1":"5"};
var secondMap = {"1":"6"};
var thirdMap = {"2":"7"};
var finalMap = {...firstMap, ...secondMap, ...thirdMap};
// finalMap: {"1":"6", "2":"7"};
Notice that key "1" with value "5" will be overwritten with "6".

Is there an easier way than this to edit a table in my database?

I’m trying to update a row in a table. I found a way to make it work but I’m wondering if there is an easier or better way. Please help me with any ideas that you may have. Thanks
Here’s the code…
var courseRubics = from r in db.Rubrics where r.DepartmentID == 2 select r;
var selectedRubrics = courseRubics.Select(r => r.RubricID);
List<int> rubricsList = selectedRubrics.ToList();
foreach (var rub in courseRubics.ToList())
{
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
Rubric updateRubic1 = (Rubric)aRubic1;
int rubric1 = Convert.ToInt32(totlrubric[0]);
updateRubic1.Weight = rubric1;
Rubric aRubic2 = db.Rubrics.Find(2);
Rubric updateRubic2 = (Rubric)aRubic2;
int rubric2 = Convert.ToInt32(totlrubric[1]);
updateRubic2.Weight = rubric2;
Rubric aRubic3 = db.Rubrics.Find(4);
Rubric updateRubic3 = (Rubric)aRubic3;
int rubric3 = Convert.ToInt32(totlrubric[2]);
updateRubic3.Weight = rubric3;
}
db.SaveChanges();
}
Couple of ideas / observations
Your initial set of selects could be narrowed down to (sorry for the alternate syntax)
var list = db.Rubrics.Where(r => r.DepartmentId == 2).Select(x => x.RubricId).ToList()
You iterate over foreach (var rub in courseRubics.ToList()) but don't seem to be doing anything with rub
The contents of the if... could be tightened up a little. I don't see any reason why you need to do the casting or why you can't just inline the creation of rubricX,
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
aRubic1.Weight = Convert.ToInt32(totlrubric[0]);
Rubric aRubic2 = db.Rubrics.Find(2);
aRubic2.Weight = Convert.ToInt32(totlrubric[1]);
Rubric aRubic3 = db.Rubrics.Find(4);
aRubic3.Weight = Convert.ToInt32(totlrubric[2]);
}

Setting Actionscript Object Keys

If I have an array, I can set the keys by doing the following:
var example:Array = new Array();
example[20] = "500,45";
example[324] = "432,23";
If I want to do something with Objects, how would I achieve this?
I tried the following:
var example:Object = [{x:500, y:45}, {x:432, y:23}]; // Works but keys are 0 and 1
var example:Object = [20: {x:500, y:45}, 324: {x:432, y:23}]; // Compile errors
var example:Object = [20]: {x:500, y:45}, [324]: {x:432, y:23}; // Compile errors
var example:Object = [20] {x:500, y:45}, [324] {x:432, y:23}; // Compile errors
Is there a good way to achieve this?
I understand I could do this:
var example:Object = {id20 : {x:500, y:45}, id324: {x:432, y:23} };
But it doesn't suit me.
The [] notation has the same meaning of doing a new Array() so when you are doing:
var example:Object = [{x:500, y:45}, {x:432, y:23}];
you are in fact creating an array with two elements who are object {x:500, y:45} and {x:432, y:23}.
If you want to create an object with key 20 and 324 use the {} notation who is the same a new Object()
So your example became =>
var example:Object = {20: {x:500, y:45}, 324: {x:432, y:23}};
You can do the same as your first example using an Object instead of an Array:
var example:Object = new Object();
example[20] = "500,45";
example[324] = "432,23";

Is there an easier way to modify a value in a subsubsub record field in Erlang?

So I've got a fairly deep hierarchy of record definitions:
-record(cat, {name = '_', attitude = '_',}).
-record(mat, {color = '_', fabric = '_'}).
-record(packet, {cat = '_', mat = '_'}).
-record(stamped_packet, {packet = '_', timestamp = '_'}).
-record(enchilada, {stamped_packet = '_', snarky_comment = ""}).
And now I've got an enchilada, and I want to make a new one that's
just like it except for the value of one of the subsubsubrecords.
Here's what I've been doing.
update_attitude(Ench0, NewState)
when is_record(Ench0, enchilada)->
%% Pick the old one apart.
#enchilada{stamped_packet = SP0} = Ench0,
#stamped_packet{packet = PK0} = SP0,
#packet{cat = Tag0} = PK0,
%% Build up the new one.
Tude1 = Tude0#cat{attitude = NewState},
PK1 = PK0#packet{cat = Tude1},
SP1 = SP0#stamped_packet{packet = PK1},
%% Thank God that's over.
Ench0#enchilada{stamped_packet = SP1}.
Just thinking about this is painful. Is there a better way?
As Hynek suggests, you can elide the temporary variables and do:
update_attitude(E = #enchilada{stamped_packet = (P = #packet{cat=C})},
NewAttitude) ->
E#enchilada{stamped_packet = P#packet{cat = C#cat{attitude=NewAttitude}}}.
Yariv Sadan got frustrated with the same issue and wrote Recless, a type inferring parse transform for records which would allow you to write:
-compile({parse_transform, recless}).
update_attitude(Enchilada = #enchilada{}, Attitude) ->
Enchilada.stamped_packet.packet.cat.attitude = Attitude.
Try this:
update_attitude(E = #enchilada{
stamped_packet = (SP = #stamped_packet{
packet = (P = #packet{
cat = C
})})}, NewState) ->
E#enchilada{
stamped_packet = SP#stamped_packet{
packet = P#packet{
cat = C#cat{
attitude = NewState
}}}}.
anyway, structures is not most powerful part of Erlang.

Resources