Why my function doesn't get my int var?! AS3 - arrays

I have a trouble. I got my dear var myVar:int and an arr:Array. I want to use them to execute my function.
s1 and indice1 are array and integer values as I was defined in my program.
getIndex(s1, indice1);
function getIndex(arr:Array, index:int = 0):void {
for (var n:int = 0; n <= arr.length; n++) {
if (String(arr[n]).indexOf(">") >= 0) {
index = n;
trace(n);
arr[n] = String(arr[n]).substring(1);
}
}
}
now i get rigth results for my Array but i have a 0 for my index var.
Can someone help me?
I mean i need a dynamic way to assing index values of differents arrays to diferents indices so I can use my function to retrieve and index and save it for any Array I have

Your question is pretty unclear.
However, to answer (what I THINK you are asking),
You can not access variable index outside of the function. This is because the type int is not saved as a reference in AS3. To get index outside, you will have to do either of the following:
a) Assign the value of index to a global variable e.g.:
var gIndex:int;
function getIndex(arr:Array,index:int = 0):void{
//function contents
gIndex=index;
}
//This way you can access index as gIndex;
b) Return the variable index from the function
function getIndex(arr:Array,index:int = 0):int{
//function contents
return index;
}
//this way you can access index as getIndex(s1, indice1);

Your search string is available in arr[0] So that it returns 0.

Ok, I solve it.
Well it isn't what I want to but look.
I decided to return n value from my function and assing it to my var.
indice1 = getIndex(s1);
function getIndex(arr:Array){
for(var n:int=0; n<= arr.length; n++){
if( String(arr[n]).indexOf(">") >= 0){
return n;
arr[n]= String(arr[n]).substring(1);
}
}
}
So I got n value on my var and that's it. thanks Anyway =D

Related

sorting algorithm challenge using 'for of loop' instead of 'for loop' - issue

I have solved an algorithm using a for loop, but I have been trying to use a for of loop, to make it easier to read, but i'm not getting the same output when I used a traditional for loop?
const sortByHeight = (a)=>{
const array2 = a.filter(num => {
if (num !== -1){
return num
}
}).sort((a,b) => a-b)
let indexVal = 0;
for ( let num of a){
if(num !== -1 ){
num = array2[indexVal]
indexVal++
}
}
return a;
//for loop does work
// for ( let i=0; i < a.length; i++){
// if(a[i] !== -1 ){
// a[i] = array2[indexVal]
// indexVal++
// }
// }
// return a;
console.log(sortByHeight([-1, 150, 190, 170, -1, -1, 160, 180]));}
num is a local variable inside the loop. Assigning to it does not change the corresponding element in the array. This is similar to how assigning to a function parameter has no effect on the argument that was passed to the function.
Your best bet is just to stick to indexing the array like you were before. Use for...of when you want to iterate over an array and just read it. It doesn't allow for overwriting elements like you're trying to do though.
If you really wanted to use for...of, you could zip the array to be sorted with another array of indices so you can index when required, but you're losing any readability benefit of for...of at that point.

Printing Array Items by Index Number in ImageJ (Fiji)

I'm trying to figure out how I can access an item in an array by it's index number. I've written a script which will generate an array, with some number of variables. There's an old script on the imageJ mailing list archive (shown below) which is able to print given index value for a known value within the array, but is there a way to find the value within the array itself? I.e. if I have the user input the number of values that should be in the array, can I have the macro call up the values in the array from that?
My array generator:
Dialog.create("Time Point Input");
Dialog.addNumber("How many time points?", 0)
Dialog.addString("What are your time points (comma separated, no spaces)?:",0);
Dialog.show();
time = Dialog.getNumber();
points = Dialog.getString();
Fpoints = newArray(points);
Where the readouts could be something like:
time = 4
points = 5,10,12,27
Fpoints[0] = 5
Fpoints [1] = 10
Fpoints [2] = 12
Fpoints [3] = 27
Calling up index from array number value example code:
arr = newArray(1,5,3,12);
i = index(arr, 5);
print("index = "+i);
function index(a, value) {
for (i=0; i<a.length; i++)
if (a[i]==value) return i;
return -1;
}
Thank you!
I am not 100% sure if I get your question right.
but is there a way to find the value within the array itself?
The problem is that you cannot create an Array with points since it's a String.
Try something linke:
Fpoints = split(points, ',');
Then you can iterate over Fpoints with a loop, or use you index function to get an index of a given value.
for (i = 0; i < Fpoints.length; i++) {
print(Fpoints[i]);
}

FOR loop in C for specified i values

I'm trying to make a FOR loop in c with specific i values for example i=2,6,8,12,16,20,22,26,34.. I just don't know how to specify increments as they are not equal. This code is not good for me.
for (i=2,i<=30,i++) {
}
Maybe with specified array of values in i and then run for loop with reference to that array?
Any help?
Your for-loop syntax needs to be fixed.
The first option is to use a custom function:
for (i=2; i <= 30; i = foo(i)) { ... }
where foo is a function which takes the current value of i and returns the next value. You will need to come up with a proper definition of foo yourself.
The other approach can be to put all these values in an array and use the array as indices:
int indices[ MAX_INDICES ] = { 2, 6, ... };
for (size_t j = 0; j < MAX_INDICES; ++j) {
/* instead of using 'j' use indices[ j ] now on */
}
The amount you want to add seems not to be allways the same. Because of that I would suggest to put the Values into an array and loop through it with an index.
pseudocode:
int vals[10];
// fill the values
vals[0] = 2;
vals[1] = 6;
vals[2] = 8;
...
for(int i = 0; i < 10; ++i)
{
// work
}
Correct, you want to put the indices in an array:
static const int indices[] = { 2,6,8,12,16,20,22,26,34 };
for (int i = 0; i != sizeof(indices) / sizeof(indices[0]); ++i) {
const int j = indices[i];
...
}
Is there any pattern for the values of i? If not, and the number of possible values you need is small enough, you would need to create an array and the iterate across the array, something like:
int myValue[9] = {2,6,8,12,16,20,22,26,34};
int i;
for(i=0; i < 9; i++)
{
printf("myValue[%d] = %d\n", i, myValue[i]);
}
For a step of 2 you can make it like this.
for (i=2,i<=30,i+=2) {
}
What are the exact constraints? Seems to increment by 4 most of the time but then there are variations.
Given that, it seems the best approach would be to put the values you need into an array and use them that way, or alternatively define a function that can generate these values and then access them that way (rather than indexing through your array of values).
Also, note the parts inside the for-loop are separated by ; not ,

flash as3 using an array inside of Math.max() function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the best way to get the minimum or maximum value from an Array of numbers?
Hi - I'm trying to use flash's Math.max() functionality to find the highest of a set of numbers. Normally, these are input via a comma delimited string of numbers, but I need to have it loop through a series of numbers in an array. What is the proper syntax for this? I tried:
var maxMemberWidth = int(Math.max(
for (var k=0;k<memberClips.length;k++){
memberClips[i].memeberWidth;
}
));
but that's obviously not right. I have a feeling the answer involves running some sort of separate function and returning the values to this function, but I haven't quite gotten the syntax right yet.
Your aproach is not ALL wrong, you can do this:
// note the apply in the next line!
var maxMemberWidth:int = int(Math.max.apply(null, AllMembersWidth()));
...
private function AllMembersWidth():Array
{
var widths:Array = [];
for (var k:int = 0; k < memberClips.length; k++)
widths[k] = memberClips[k].memeberWidth;
return widths;
}
This works even when you have an empty array (it returns -Infinity)
You can try this instead:
var max_number:Number = memberClips[0];
for( var i:int = 1; i < memberClips.length; i++ )
{
max_number = Math.max( max_number, memberClips[i] );
}
What this does is initializes the maximum number to the first element of your array and then loops through all the elements in your array until it reaches the end of the array. You'll then have the maximum number you're seeking. The index variable i is initialized to 1 because you don't need to compare the first element against itself. You should take note that this code also assumes that you have at least one element in your array.
More condensed version, not as sexy looking as it would be on ruby but it works.
var widths:Array = clips.map( function(o:*, i:int, a:Array):* { return o.width; } );
var max:Number = Math.max.apply(null, widths);
var min:Number = Math.min.apply(null, widths);

AS3 Array Object Filtering Question

I have an array of objects. What I would like to do is get the last index of an object whose property equals a certain value.
Ex:
// this occurs on a mouse click
var stockObj:Object = new Object();
stockObj.ID = "an_id";
stockObj.category = "a_category";
array.push(stockObj);
//psuedo
trace(array.lastIndexOf(stockObj.category=="a_category"));
I would like this to trace the index of the object whose category property equals "a_category"
function searchCategory(arr:Array, cat:String):int {
for (var i:int = arr.length - 1; i >= 0; i--) {
if (arr[i].category == cat) { // assuming array objects contains category
return i;
}
}
return -1; // no match
}
last index of searches on a string not an array:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#lastIndexOf%28%29
what you will need to do is run through the Array which will take O(n) time and compare to see which at which index has the object with category name "a_category"
for(int i = 0;i<array.length;i++){
if(array[i].category = "a_category")
maxIndex = i;
}
}
There is actually a better way to loop through all obj in an array of obj, but I can't remember it atm hopefully someone can comment that in but I think its something like
for (x in array){
...
}
anyways using that logic, it would be a lot faster if you reverse it, so you start at the end of the array and return the index with the first occurrence of the obj with category "a_category"

Resources