Get element in Array - arrays

In the execution of my code, how do I get the r.diameter element, which is stored in an array, to another code function?
public static RebarProps [] GetRebarsHor (Element el, Document doc)
{
var rebars = RebarHostData.GetRebarHostData(el).GetRebarsInHost();
var rlst = new List<RebarProps>();
var klen = RevitUnits.ConvertToDisplayUnitsLength(doc);
foreach (var rebar in rebars)
{
if (IsHor(rebar, out var numesegs, out var segment))
{
var r = new RebarProps();
r.diameter = GetRebarDiameter (rebar) * klen;
r.spacing = rebar.MaxSpacing * klen;
r.isStirrup = numesegs > 1;
rlst.Add(r);
}
}
return rlst.ToArray();
}
public static double GetConfinedWidth (Element el, Document doc)
{
var array = GetRebarsHor (el, doc);
foreach( var item in array)
{
}
}
Thanks!

I would assume item.diameter.

Related

Sorter Function in List never called

I tried to Implement the qx.ui.list.core.IListDelegate.sorter function in a qx.data.controller.List. I set the Delegate of the controller with the specific sort Function. The Problem is, that the sorter Function is never been called.
var list = new qx.ui.form.List();
var rawData = [];
for (var i = 0; i < 10; i++) {
rawData.push(i);
}
var data = new qx.data.Array(rawData);
var listController = new qx.data.controller.List(data, list);
listController.setDelegate({
sorter : function(a, b) {
console.log("test");
if (Number(a.getLabel()) > Number(b.getLabel())) {
return 1;
}
if (Number(a.getLabel()) < Number(b.getLabel())) {
return -1;
}
return 0;
}
});
// Document is the application root
var doc = this.getRoot();
doc.add(list,
{
left : 100,
top : 50
});
Here is the example in the qx Playground: tinyurl.com/y6udac8g
qx.ui.list.core.IListDelegate.sorter is used to configure qx.ui.list.List and not qx.ui.form.List.
Check the example in the documentation to use the sorter function of the qx.ui.list.List class
//create the model data
var rawData = [];
for (var i = 0; i < 2500; i++) {
rawData[i] = "Item No " + i;
}
var model = qx.data.marshal.Json.createModel(rawData);
//create the list
var list = new qx.ui.list.List(model);
//configure the lists's behavior
var delegate = {
sorter : function(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
};
list.setDelegate(delegate);
//Pre-Select "Item No 20"
list.getSelection().push(model.getItem(20));
//log selection changes
list.getSelection().addListener("change", function(e) {
this.debug("Selection: " + list.getSelection().getItem(0));
}, this);

How do i get Object index in an array

I have seen many threads about this but my problem doesn't solved. This may be a simple way but i have no idea...
I'm trying to get Objects indices in an array like so :
var test:Array = new Array();
for (var row:Number = 0; row < 2; row++) {
test[row] = [];
for (var column:Number = 0; column < 3; column++) {
test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
test[row][column] = new ballShape(column, column, row);
addChild(test[row][column]);
}
}
function objClicked(evt:MouseEvent):void {
// Here must return Object index in array
}
P.S :
I can get items index in int array, but i don't know about objects.
Any ideas would be appreciated.
Edit :
ballShape.as
package {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.MouseEvent;
import fl.motion.Color;
public class ballShape extends Sprite {
private var shapeId:uint;
private var currentPosition:uint;
private var arrayPosition:uint;
private var color:Number;
public function ballShape(Id:uint, currPos:uint, arrPos:uint) {
setId(Id);
setArrayPos(arrPos);
setCurrentPos(currPos);
//trace("Array : " + arrPos);
//trace("Curr : " + currPos);
if (arrPos == 0) {
var posX:uint = 60;
} else {
var posX:uint = (arrPos + 1) * 60;
}
if (currPos == 0) {
var posY:uint = 42;
} else {
var posY:uint = (currPos + 1) * 42;
}
if (arrPos == 0) {
color = 0xFF0000;
} else {
color = 0x00FF00;
}
graphics.beginFill(color, 1.0);
graphics.drawCircle(posX, posY, 20);
graphics.endFill();
this.addEventListener(MouseEvent.CLICK, Clicked);
}
public function setId(Id:uint):void {
shapeId = Id;
}
public function getId():uint {
return shapeId;
}
public function Clicked(evt:MouseEvent):void {
//return getId();
trace("Ball id is " + getId());
trace("Array id is " + getArrayPos());
trace("PositionInArray id is " + getCurrentPos());
//return arrayPosition;
}
public function setCurrentPos(Pos:uint):void {
currentPosition = Pos;
}
public function getCurrentPos():uint {
return currentPosition;
trace(currentPosition);
}
public function setArrayPos(arrayPos:uint):void {
arrayPosition = arrayPos;
}
public function getArrayPos():uint {
return arrayPosition;
trace(arrayPosition);
}
public function addBead(arrayId, currPos):void {
}
}
}
I would suggest adding row and column as public variables in your ballShape class.
That way you can get them like this:
function objClicked(evt:MouseEvent):void {
trace(ballShape(evt.target).getCurrentPos(), ballShape(evt.target).getArrayPos());
}
Maybe turn this two lines around:
test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
test[row][column] = new ballShape(column, column, row);
to be :
test[row][column] = new ballShape(column, column, row);
test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
Try something like:
protected function objClicked(e:MouseEvent):void {
for (var row: int=0; row < test.length; row++) {
var column:int = (test[row] as Array).indexOf(e.currentTarget);//use currentTarget so you don't try to match on internal Interactive Objects!
if (column>-1) {
trace(row, column);
return;//or break, if you need to do something else below this loop
}
}
}

looping and returning a array

I am in need of some help. I am very new to actionscript and what I am trying to do is calculate the amount of apples from the array and then trace it in the main function. Right now I am getting an error on the return and that error is 1067: Implicit coercion of a value of type String to an unrelated type Number. Now I have tried to switch out the number and string but I still get the error.
public class extends Sprite
{
public function()
{
var fruitNames:Array = ["apple", "peach","banana","pear","guava","apple","peach"];
var totalApples:Number = countingApples(fruitNames);
trace("You have " + totalApples);
}
private function countingApples(fruitNames:Array):Number
{
var total:String = "apple";
for(var i:uint=0; i<7; i++)
{
total += Number[i];
}
return total;
}
}
public class extends Sprite
{
public function()
{
var fruitNames:Array = ["apple", "peach","banana","pear","guava","apple","peach"];
var totalApples:Number = countingApples(fruitNames);
trace("You have " + totalApples);
}
private function countingApples(fruitNames:Array):Number
{
var total:uint = 0;
var i:String = new String();
for(i in fruitNames) {
if (fruitNames[i] == "apple") {
total = total + 1;
}
}
return total;
}
}
That error is because you're trying to return total, which is a String, but you've set the countingApples function's return type as Number. Here's an example which will do what you want, but I think you should try writing your own version again so that you know what's going on:
private function countingApples(fruitNames:Array):Number
{
var total:uint = 0;
var targetFruit:String = "apple";
var totalFruit:uint = fruitNames.length;
for(var i:uint = 0; i < totalFruit; i++)
{
if (fruitNames[i] == targetFruit) {
total++;
}
}
return total;
}

Actionscript 3: Trying to create a randomized array

im trying to create a randomized array that will change the position of my pictures(in the tilelist) each time the application is launched. Hope you understand what im looking for, and i dont really understand how to link code correctly here :/
I think its easier simply copying into flash and view from there
thanks :)
Here's my code:
flash.events.MouseEvent;
btn_back.addEventListener(MouseEvent.CLICK, ftilbake);
function ftilbake(evt:MouseEvent)
{
gotoAndStop(1);
}
var heroArray:Array = new Array();
var randomizeArray:Array = new Array();
createArrays()
function createArrays()
{
heroArray[0] = new Array("Rumble","Garen","Lulu","Corki","Warwick");
heroArray[1] = new Array("Bilder/Champions/Rumble.jpg","Bilder/Champions/Garen.jpg","Bilder/Champions/Lulu.jpg","Bilder/Champions/Corki.jpg","Bilder/Champions/Warwick.jpg");
heroArray[2] = new Array("Bilder/Champions/Rumble1.jpg","Bilder/Champions/Garen1.jpg","Bilder/Champions/Lulu1.jpg","Bilder/Champions/Corki1.jpg","Bilder/Champions/Warwick1.jpg");
heroArray[3] = new Array("the Mechanized Menace","the Might of Demacia","the Fae Sorceress","the Daring Bombardier","the Blood Hunter");
heroArray[4] = new Array(0,0,0,0,0);
heroArray[5] = new Array("Rumble.wav","Garen.wav","Lulu.wav","Corki.wav","Warwick.wav");
randomizeArray[0] = new Array();
randomizeArray[1] = new Array();
randomizeArray[2] = new Array();
randomizeArray[3] = new Array();
randomizeArray[4] = new Array();
//randomizing the positions in the array(?)
var randomPos:int = 0;
for (var i:int = 0; i < heroArray.length; i++)
{
randomPos = int(Math.random() * heroArray[0].length);
while (randomizeArray[randomPos][0] != null)
{
randomPos = int(Math.random() * heroArray.length);
}
}
}
var totalKlikk:int = 0;
for (var teller1:int = 0; teller1 <heroArray[0].length; teller1++)
{
leagueChamps.addItem({label:heroArray[0][teller1], source:heroArray[1][teller1]});
}
leagueChamps.columnWidth = 80;
leagueChamps.rowHeight = 80;
leagueChamps.columnCount = 5;
leagueChamps.rowCount = 1;
leagueChamps.direction = "horizontal";
leagueChamps.addEventListener(MouseEvent.CLICK, bildeKlikk);
function bildeKlikk(evt:MouseEvent)
{
var element:Object = leagueChamps.selectedItem;
var fil:String = element.source;
txtChHero.visible = false;
totalKlikk++;
if (totalKlikk <11)
{
for (teller1 = 0; teller1 <heroArray[0].length; teller1++)
{
if (heroArray[1][teller1] == fil)
{
heroArray[4][teller1]++;
if (heroArray[4][teller1] == 1)
{
txtBox1.visible = true;
txtBox2.visible = true;
leagueShow.source = heroArray[2][teller1];
txtBox1.text = heroArray[0][teller1];
txtBox2.text = heroArray[3][teller1];
}
if (heroArray[4][teller1] == 2)
{
txtBox1.visible = true;
txtBox2.visible = true;
leagueShow.source = heroArray[2][teller1];
txtBox1.text = heroArray[0][teller1];
txtBox2.text = heroArray[3][teller1];
heroArray[5][teller1].play();
}
if (heroArray[4][teller1] == 3)
{
bildeKlikk3();
}
}
}
}
else
{
txtChHero.visible = true;
txtChHero.text = "Du har klikket følgende mange ganger på de forskjellige bildene:";
txtH1.text = heroArray[4][0]
txtH2.text = heroArray[4][1]
txtH3.text = heroArray[4][2]
txtH4.text = heroArray[4][3]
txtH5.text = heroArray[4][4]
txtBox1.visible = false;
txtBox2.visible = false;
leagueShow.visible = false;
}
}
function bildeKlikk3()
{
txtBox1.visible = true;
txtBox2.visible = true;
leagueShow.source = heroArray[2][teller1];
txtBox2.text = "Ikke mer informasjon";
}
txtBox2.visible = false;
txtBox1.visible = false;
Array randomization is something which comes up very frequently in all my projects so I ended up creating a static method in my Array utility class for it.
It uses the Fisher–Yates shuffle which is supposed to be the most unbiased (and efficient?) algorithm for shuffling the content of an array. There could be faster ways of doing it (like using the array.sortOn() method, but I am not sure how unbiased a result they get compared to this one.)
The shuffle method:
/**
* shuffle the given array
* #param array
* #return
*/
public static function shuffle(array:Array):Array
{
var index :int;
var item :*;
var limit :int = array.length as int;
for (var i:int = limit-1; i >= 0 ; --i)
{
index = Math.floor(Math.random() * (i + 1));
item = array[index];
array[index] = array[i];
array[i] = item;
}
return array;
}
Example:
var myArray:Array = new Array("Red","Orange","Yellow","Green","Blue");
myArray = ArrayUtils.shuffle(myArray);
where ArrayUtils is the name of the Array Utility class I use. You can simply use the function directly if you don't want to use a utility class of course.
Try this
while (heroArray.length > 0) {
randomizeArray.push(heroArray.splice(Math.round(Math.random() * (heroArray.length - 1)), 1)[0]);
}
Just instatiate randomizeArray though, and don't fill it with empty Arrays like in ur source!
I recommend creating a single object to hold related data, and using a single Array/Vector of that object type.
To answer your question, randomizing an array is fairly easy to do with array.sort() and a random selection function. This method can also be used to randomize any array. you don't need to splice arrays or iterate either.
function sortOnRandom(a:Object, b:Object):Number{
if(Math.random() > 0.5){
return 1;
}else{
return -1;
}
}
myArray.sort(sortOnRandom);

How can I get the number of elements in a sparse array in Actionscript?

Actionscript uses sparse arrays, so I can have an array like this:
var myArray:Array = new Array();
myArray[0] = "foo";
myArray[22] = "bar";
Now myArray.length will give me 23. Is there a way to get the actual number of items in the array without iterating every element?
If you don't want to iterate through the array, you can filter it:
var myArray:Array = new Array();
myArray[0] = "foo";
myArray[22] = "bar";
var numberOfItems:int = myArray.toString().split(',').filter(isItem).length;
function isItem(item:*, index:int, array:Array):Boolean
{
return item != "";
}
By using for syntax, which will iterate the definded indexes:
public static function definedCount(arr:Array):uint {
var ctr:uint = 0;
for(ix:* in arr)
ctr++;
return ctr;
}
If you need to frequently check the count of items in a sparse array, you should wrap it an a collection class that independently keeps track of the count of items. Something like:
public class IndexedCollection {
private var _arr:Array = [];
private var _itemCount:uint = 0;
public function get count():uint {
return _itemCount;
}
public function getItem(index:uint):* {
return _arr[index];
}
public function setItem(index:uint, value:*):void {
if(_arr[index] === undefined)
_itemCount++;
_arr[index] = value;
}
public function delete(index:uint):void {
if(_arr[index] === undefined)
return;
delete _arr[index];
_itemCount--;
}
}
Fastest method should be to always use the inbuilt functions.
function myFilter(item:*, index:int, array:Array):Boolean{
if(item)
{
return true;
}else{
return false;
}
}
var myArray:Array = new Array();
myArray[0] = "foo";
myArray[22] = "bar";
trace(myArray.length) // 23
var myMyNewArray:Array = myArray.filter(myFilter)
trace(myMyNewArray .length) // 2

Resources