I have a array called 'BODY_STAT_ARRAY' and in this array it holds a object called 'body_stat'. This array needs to increase a decrease in size all the time, how do i remove the last object from the array, here is a example which doesn't work
if(BODY_STAT_ARRAY.length > target_size)
{
BODY_STAT_ARRAY.slice(BODY_STAT_ARRAY.indexOf(BODY_STAT_ARRAY[BODY_STAT_ARRAY.length-1]),1)
// this line above should remove the last object in the array
}
So where am I going wrong, how do you make it work.
If you can help, I would love to find out.
The easiest way to remove the last object from an array is to call pop:
BODY_STAT_ARRAY.pop();
Slice will not modify the Original Array . It will give an another Array with the defined range .
Only Splice will modify the original Array , so as to remove or add elements .
var a:Array = [1,2,3,4];
a.slice(1,1);
trace(a); // Output is : 1,2,3,4
a.splice(1,1);
trace(a); // Output is : 1,3,4
Related
I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]
I have created my own type:
type T
name
pos
end
What i want to do is create a 2d array of this type. This is how i did it:
arr = Array{T}(10,10) #create 2d Array
This creates the 2d array (10 by 10) with all the elements being undefined. (im thinking my problem is here)
So when i try to change just one element of the array
arr[1,1].name = "Hi"
I get this error:
ERROR: UndefRefError: access to undefined reference
What how I tried to fix the issue is by creating a default instance of T and iterate through the array and set every element equal to the default.
default = T("Hi",1)
for i = 1:10
for j = 1:10
arr[i,j] = default
end
end
Now, this sets every element in the array to the default value succesfully but now the problem is that if i try to change the value of just one element of the array, every element of the array is changed to that value.
arr[2,4].name ="Hello"
After that line all the elements have the value of "Hello".
Is that not how you are supposed to change values in an array? When i do the same on an Int64 2d array everything works how i would expect.
Everything is working as it should.
arr[i,j] = default
sets arr[i,j] to the reference default which is the instance of T("Hi",1). So every single arr[i,j] is referring to the same instance of the type.
arr[1,1].name = "Hi"
does not work because when you do arr = Array{T}(10,10) you create a 10x10 empty array with the ability of holding Ts, but you haven't put any Ts in there!
Thus what you want to be doing is:
arr[i,j] = T("Hi",1)
which will both make a T and make a[i,j] refer to it. Since each line is making a new T, they will refer to different instances and act separately.
using Perl I am trying to push the elements of an array to another array, and not the whole array. But I'm not getting my goal.
I tried this:
push #tmp_entities_all, #tmp_entities;
But I got the whole small array as an element in the bigger array.
Then I tried it with a loop:
for (#tmp_entities) {push #tmp_entities_all, $_;}
But the same results, the whole #tmp_entities appears as an element, and that what I dont want.
I need one dimension in the array and not array of arrays!! Should I cast something before pushing? or what is the problem?
Thanx a lot.
Out of your comments, its obvious, that #tmp_entities contains only one element, that is an array reference to the elements that you expected to be elements of #tmp_entities.
So you perhaps declared your array with an array refence instead of using a set of elements.
The line
push #tmp_entities_all, #tmp_entities;
definitly works for a normal array.
In your case, your could try ...
push #tmp_entities_all, $tmp_entities[0];
or you simply try to initialize your array with its value like
my #tmp_entities = ( 1, 2, 3 ); # initialize array with 3 elements of type int
instead of
my #tmp_entities = [ 1, 2, 3 ]; # initialize array with 1 element that is an array reference with 3 elements of type int
I know, that this is the case, because this is why your for-loop sample works with #$_ ;D (it is equivalent to push #tmp_entities_all, $tmp_entities[0]; in this situation).
I want to slice an array to create a new array containing all but the first entry in the array.
This is what i tried:
#arguments = $splittedCommands[1..-1];
Which gives me an empty result.
1 should be the second entry and -1 should be the last, so why doesn't this work?
You should use # in front of array when slicing an array ($ tells that your're accessing single scalar value inside array), so
my #arguments = #splittedCommands[ 1 .. $#splittedCommands ];
Second, your range should be either -$#splittedCommands .. -1 or 1 .. $#splittedCommands with later being more common and straightforward.
Easiest may be to assign it into another list, using
( undef, my #commands ) = #splittedCommands;
Assigning into undef here throws away the first result, then the remainder goes into #commands
Another approach could be to assign the lot and then remove the first
my #commands = #splittedCommands;
shift #commands;
This could also be simplified if you didn't need to keep the original array around any more; just shift the first item off #splittedCommands then use it directly.
If the original array must be left intact then the obvious way seems to be to copy the whole array and remove the first element.
shift(my #commands = #splittedCommands)
will do just fine
I am currently trying to figure out how to design some sort of loop to insert data into an array sequentially. I'm using Javascript in the Unity3D engine.
Basically, I want to store a bunch of coordinate locations in an array. Whenever the user clicks the screen, my script will grab the coordinate location. The problem is, I'm unsure about how to insert this into an array.
How would I check the array's index to make sure if array[0] is taken, then use array[1]? Maybe some sort of For loop or counter?
Thanks
To just add onto the end of an array, just use .push().
var myArray = [];
var coord1 = [12,59];
var coord2 = [87,23];
myArray.push(coord1);
myArray.push(coord2);
myArray, now contains two items (each which is an array of two coordinates).
Now, you wouldn't do it this way if you were just statically declaring everything as I've done here (you could just statically declare the whole array), but I just whipped up this sample to show you how push works to add an item onto the end of an array.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push for some reference doc on push.
In case you need to know the array's length when reading the array in the future, you can use the .length attribute.
var lengthOfArray = myArray.length;
Using the .push() method as suggested by jfriend00 is my recommendation too, but to answer your question about how to work out what the next index is you can use the array's length property. Because JavaScript arrays are zero-based The length property will return an integer one higher than the current highest index, so length will also be the index value to use if you want to add another item at the end:
anArray[anArray.length] = someValue; // add to end of array
To get the last element in the array you of course say anArray[anArray.length-1].
Note that for most purposes length will give the number of elements in the array, but I said "one higher than the current highest index" above because JavaScript arrays are quite happy for you to skip indexes:
var myArray = [];
myArray[0] = "something";
myArray[1] = "something else";
myArray[50] = "something else again";
alert(myArray.length); // gives '51'
// accessing any unused indexes will return undefined